Use Constrol's Tag : Controls « GUI « VB.Net Tutorial






Use Constrol's Tag
imports System
imports System.Drawing
imports System.Windows.Forms

public class Tags : inherits Form
  private lbl as Label
  public sub New()
    Size = new Size(300,200)

    lbl = new Label()
    lbl.Text = "Label..."
    lbl.AutoSize = true
    lbl.Parent = me
    lbl.Location = new Point(10,10)

    dim theEnum as new FontStyle()
    dim theStyles as FontStyle() = CType([Enum].GetValues(theEnum.GetType()), FontStyle())

    dim i as integer = 1
    dim style as FontStyle

    for each style in theStyles
      dim btn as new Button()
      btn.Parent = me
      btn.Location = new Point(25,25 * i)
      btn.Size = new Size(75,20)
      btn.Text = style.ToString()
      btn.Tag = style
      AddHandler btn.Click, AddressOf btn_Click
      i += 1
    next
  end sub

  public shared sub Main() 
    Application.Run(new Tags())
  end sub

  private sub btn_Click(ByVal sender as object,ByVal e as EventArgs)
    dim btn as Button = CType(sender, Button)
    dim fs as FontStyle = CType(btn.Tag, FontStyle)
    lbl.Font = new Font(lbl.Font, fs)
  end sub
end class








14.73.Controls
14.73.1.Add Label to a formAdd Label to a form
14.73.2.Add TextBox to Form in codeAdd TextBox to Form in code
14.73.3.Check Control type
14.73.4.Cast event sender to control
14.73.5.Read and Save controls on a form to a fileRead and Save controls on a form to a file
14.73.6.Set Control ParentSet Control Parent
14.73.7.Add the Button control to the Form Controls collectionAdd the Button control to the Form Controls collection
14.73.8.Use Constrol's TagUse Constrol's Tag
14.73.9.Hide a controlHide a control