Controls Collection for Visual Basic 6.0 Users
The Visual Basic 6.0 Controls collection is replaced by theclass in Visual Basic 2005.
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 2005, the ControlCollection class replaces the Controls collection. Forms have a default ControlCollection class that can be accessed using the syntax Me.Controls.
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.
In Visual Basic 2005, themethod of the ControlCollection class requires that the controls were created using the New keyword before being added to the collection.
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 2005 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 2005, these controls are replaced by theand or components; components are not members of the 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 2005 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.
Code Changes for the Controls Collection
The following examples illustrate the differences in coding techniques between Visual Basic 6.0 and Visual Basic 2005.
Code Changes for Adding and Removing Controls
The following code illustrates differences between the Visual Basic 6.0 Controls collection and the Visual Basic 2005 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 2005 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 allcontrols. This example assumes that the CheckBox controls are situated on or controls rather than on the form. In the Visual Basic 2005 example, since the form's 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 2005 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
Because of differences between Visual Basic 6.0 and the Visual Basic 2005 Controls collections, calls to the Add method are not upgraded. You must add code to recreate the behavior of your application using the new Add method.