Windows Forms Controls in VB.NET
In this article you will leanr how to Work with Controls using VB.NET in windows programming
Consistency and adaptability are two often-undeclared but implicit software design goals. In an object-oriented world, a frequently employed mechanism to achieve these objectives is inheritance.
In the System.Windows.Forms namespace, all controls derive from the Control class. The raw Control class is not normally instantiated but rather forms the basis for further refining the user interface hierarchy.
The Control class implements the basic functionality of controls and, where appropriate, provides for members to be overridden. This approach promotes not only reusability but also standardization. This can be seen in two Control class properties, Name and Text. The Name property, the equivalent to a control ID in Win32, applies to all controls regardless of type and therefore is not declared "virtual," whereas a Text property implementation differs depending on the type and can be overridden. For example, Form.Text refers to the caption in the title bar, while TextBox.Text returns the user's keyboard input.
In addition to many controls directly deriving from the Control class, a number of classes act as intermediaries to collect controls into what loosely could be termed a behavioral unit. Figure 9.16 shows a number of these intermediate classes.
Figure 9.16: Intermediate Classes
The ButtonBase class, for instance, is the root of the Button, CheckBox, and RadioButton classes, which exhibit a similar behavior. The ButtonBase class handles common chores such as raising mouse and focus events. Other classes that use this method of control intercession will be pointed out as they arise.
Another gauge of consistency in the Windows.Forms namespace is its treatment of collections. While objects range from the general to the very specific, the means of accessing an object in a collection remains consistent throughout the namespace. Each collection in the namespace has an indexer, as well as identical methods to add and remove objects. The most commonly referenced collection is the nested Control.ControlCollection, accessible through the read-only property called Controls. This is a container for Control objects.
Some of the properties of the Control classes are defined in Table 9.2.
Table 9.2: Control Class Properties
Some Control class methods are itemized in Table 9.3.
Table 9.3: Control Class Methods
The Control class implements basic mouse and keyboard events, some of which are defined in Table 9.4.
Table 9.4: Control Class Events
Besides the methods defined in Table 9.4, there are overridable methods for raising events programmatically, such as OnClick, OnEnter, and OnKeyUp. When overriding any of these eventtriggering methods, the base class's method must be called, so that any registered delegate receives the event.
Table 9.5 lists some of the common control classes.
Table 9.5: Windows Forms Common Control Classes
As has been seen earlier, you can create these controls programmatically as well as from the VS.NET Form Designer. The Form Designer allows you to drag and drop controls from the toolbox onto a form. Here we will show you how to create a control manually and set its properties.
Creating a Control
Use a constructor to create an instance of a control. Most controls are constructed with a default constructor, that is, a constructor with no parameters. Here is an example of creating a Button control:
Dim btn1 As Button = New System.Windows.Forms.Button()
After creating the object, set the control's properties. The code in Listing 9.5 sets the button properties.
Listing 9.5: Setting the properties of a Button Control
btn1.ImageAlign = System.Drawing.ContentAlignment.TopLeft
btn1.Dock = System.Windows.Forms.DockStyle.Left
btn1.ForeColor = System.Drawing.Color.Red
btn1.BackColor = System.Drawing.SystemColors.Desktop
btn1.DialogResult = System.Windows.Forms.DialogResult.OK
btn1.AllowDrop = True
btn1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
btn1.Size = New System.Drawing.Size(336, 568)
btn1.TabIndex = 0
btn1.Font = New System.Drawing.Font("Verdana", 10, System.Drawing.FontStyle.Bold)
btn1.Text = "Click Me"
Once you have set the properties, you can write an event handler for the control.