Using the Controls Collection

Using the Controls Collection

To add and remove controls at run time in Visual Basic 6, you can use the controls collection. For example, the following code adds a TextBox dynamically at run time:

Dim myControl As Control Set myControl = Me.Controls.Add("VB.TextBox", "myControl") myControl.Visible = True

This code adds a TextBox to the form and makes it visible. Figure 15-7 shows the result.

Figure 15-7

Adding a TextBox to a Visual Basic 6 application dynamically at run time.

It is also easy in Visual Basic 6 to remove a control dynamically. The following line of code removes the TextBox we just added:

Me.Controls.Remove "myControl"

One of the annoying things about the Visual Basic 6 model is that the controls collection has no IntelliSense, so you have to remember the methods, parameters, and ProgID of the control to add. There is no automatic upgrade for the controls collection, but it is easy to add and remove intrinsic controls in Visual Basic .NET. Here is the code for adding a TextBox to a form:

Dim c As Control c = New TextBox() c.Name = "myControl" Me.Controls.Add(c)

In Windows Forms, controls are indexed by number, not by name. To remove a control by name, you have to iterate through the controls collection, find the control, and remove it. The following code shows how to remove the newly added control by name:

Dim c As Control For Each c In Me.Controls    If c.Name = "myControl" Then       Me.Controls.Remove(c)       Exit For    End If Next

Adding ActiveX controls dynamically at run time is a bit more work. As we discussed in Chapter 13, Visual Basic .NET creates wrappers for ActiveX controls. These wrappers must exist before a control can be added. In addition, most ActiveX controls have design-time licenses that must be present before the control can be created on the form. Visual Basic .NET compiles the license into the executable. These factors mean that the control must already be present in the project before it can be added dynamically at run time. One way to do this is to add a dummy form to the project and put all ActiveX controls that will be added dynamically onto this form. After you ve created this dummy form, you can add the ActiveX control dynamically to any form in your project. The following code shows how to add a Windows Common Controls TreeView control dynamically to a form (the project already has a dummy form with a TreeView added):

Dim c As New AxMSComctlLib.AxTreeView() c.Name = "myTreeView" Me.Controls.Add(c)

The control can be removed dynamically in a manner similar to removing an intrinsic control:

Dim c As Control For Each c In Me.Controls    If c.Name = "myTreeView" Then       Me.Controls.Remove(c)       Exit For    End If Next



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net