In Visual Basic 6.0, all controls that are placed on a form are put into a collection. This collection is known as the controls collection in Visual Basic 6.0. Visual Basic .NET also supports the concept of a controls collection. However, there are certain differences in the way the controls collection is implemented in Visual Basic .NET. In Visual Basic 6.0, the add method of the controls collection is used to add the controls to the form. The control to be added is not created before calling the add method. Rather the control to be created is passed as a parameter to the add method of the controls collection. Visual Basic 6.0 allows the controls in the controls collection to be referenced by index or by name . Visual Basic .NET allows controls to be referenced only by index. Also in Visual Basic 6.0 the controls collection has a method called remove. This method removes only those controls that were previously added to the controls collection explicitly by the add method. Visual Basic .NET allows the remove method of the collections class to remove any control from the form whether the control was added using the add method during runtime or whether it was placed on the form at design time. CODE EXAMPLES Example 17 In the code example given, two controls ( Label and TextBox ) are added to the Visual Basic form using the controls collection, and they are positioned on the form in a suitable location. The whole code is put in the form load event. The following code is in the Controls-VB folder for this chapter: Private Sub Form_Load() Dim objControl1 As Control Set objControl1 = Controls.Add("VB.Label", "Label1") objControl1.Visible = True objControl1.Caption = "This is the label caption" objControl1.Left = 100 objControl1.Top = 200 Dim objControl2 As Control Set objControl2 = Controls.Add("VB.TextBox", _ "TextBox1") objControl2.Visible = True objControl2.Text = "This is the text in the textbox" objControl1.Left = 100 objControl1.Top = 800 End Sub When this Visual Basic 6.0 code is upgraded, compiling the resulting upgraded project results in many errors. First, there is a late binding for the Label control, and the caption property is not supported in Visual Basic .NET. Because the label object is late bound, there is no way for the upgrade wizard to know that the control is a label. In Visual Basic, the add control method is not supported. The controls have to be first created and only then they can be added in Visual Basic .NET. This means that the developer would have to first create the Label and TextBox controls. The following code snippet shows how the functionality seen in Visual Basic 6.0 can be achieved in Visual Basic .NET. This code is in the Controls-VB.NET-Modified folder for this chapter: Private Sub Form1_Load(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles MyBase.Load Dim label1 As New System.Windows.Forms.Label() label1.Text = "This is the label caption" label1.Left = VB6.TwipsToPixelsX(100) label1.Top = VB6.TwipsToPixelsY(200) Me.Controls.Add(label1) Dim textBox1 As New System.Windows.Forms.TextBox() textBox1.Text = "This is the text in the textbox" textBox1.Left = VB6.TwipsToPixelsX(100) textBox1.Top = VB6.TwipsToPixelsY(800) Me.Controls.Add(textBox1) End Sub In Visual Basic .NET, the controls are added via the controls collection of the form object. However, these controls have to be created before they can be added. |