Controls Collection for Visual Basic 6.0 Users
The Visual Basic 6.0 Controls collection is replaced by the Control.ControlCollection class in Visual Basic 2008.
In Visual Basic 6.0, the Controls collection is a collection with elements that represent the controls on a form or container control.
In Visual Basic 6.0, the Add method of the Controls collection is late-bound; controls are created in the Add method by specifying the Control class as an argument.
The Remove method of the Visual Basic 6.0 Controls collection could only be used for controls that were added using the Add method; the Visual Basic 2008 Control.ControlCollection class does not have this restriction.
Timer and Menu Controls
In Visual Basic 6.0, the Timer and Menu controls are members of the Controls collection. In Visual Basic 2008, these controls are replaced by the Timer and MainMenu or ContextMenu components; components are not members of the Control.ControlCollection class.
The Visual Basic 6.0 Controls collection includes controls that are children of a container control (for example, controls sited on a Frame control); the Visual Basic 2008 Control.ControlCollection class does not. To iterate through all the controls on a form, you must recursively iterate through the Controls class of each container control.
The following examples illustrate the differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.
Code Changes for Adding and Removing Controls
The following code illustrates differences between the Visual Basic 6.0 Controls collection and the Visual Basic 2008 Control.ControlCollection class.
' Visual Basic 6.0 Private Sub Command1_Click() ' Declare a new Control variable. Dim c As Control ' Create and add the new control. Set c = Controls.Add("VB.TextBox", "Text1") ' Make the new control visible. c.Visible = True ' Set the initial text. c.Text = "Hello" ' Retrieve the text from the new TextBox. If Controls.Count > 1 Then MsgBox (Controls("Text1").Text) End If ' Remove the new control. Controls.Remove (Text1) ' The following line causes a compilation error. ' You cannot remove controls added at design time. Controls.Remove (Command1) End Sub
' Visual Basic Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Create a new TextBox control. Dim TextBox1 As New System.Windows.Forms.TextBox TextBox1.Name = "TextBox1" ' Add the new control to the form's Controls collection. Me.Controls.Add(TextBox1) ' No need to set Visible property. ' Set the initial text. TextBox1.Text = "Hello" ' Retrieve the text from the new TextBox. If Me.Controls.Count > 1 Then MsgBox(Me.Controls("TextBox1").Text) End If ' Remove the new control. Me.Controls.Remove(TextBox1) ' Remove the control added at design time. Me.Controls.Remove(Button1) End Sub
Code Changes for Iterating Through the Controls Collection
The following code illustrates a function for iterating through all the controls on a form and then clearing all CheckBox controls. This example assumes that the CheckBox controls are situated on GroupBox or Panel controls rather than on the form. In the Visual Basic 2008 example, since the form's Controls collection only includes controls situated directly on the form, the function recursively calls itself for any control that has children.
' Visual Basic 6.0 Private Sub ClearChecks() For Each Control in Me.Controls If TypeOf Control Is CheckBox Then Control.Value = vbUnchecked End If Next End Sub
' Visual Basic Private Sub ClearChecks(ByVal Container As Control) Dim ctl As Control Dim chk As CheckBox For Each ctl In Container.Controls If TypeOf ctl Is CheckBox Then chk = ctl chk.Checked = False End If ' Recursively call this function for any container controls. If ctl.HasChildren Then ClearChecks(ctl) End If Next End Sub