Adding Controls Dynamically


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 16.  Designing User Interfaces

Adding controls dynamically allows you complete flexibility over your user interface. VB6 supported dynamic control addition, but the addition of controls was inconsistent with the model of declaring an object, creating a new instance of the object, and adding that object to a parent container.

VB6 supported adding controls to a form using a syntax that is more consistent with late-binding COM syntax than an object creation syntax (see Listing 16.7 for a VB6 example). Dynamic control creation in Visual Basic .NET is close enough that you should be able to make the transition easily enough (see Listing 16.8).

Listing 16.7 Dynamic control creation in VB6
  1:  Option Explicit  2:   3:  Private WithEvents Button As CommandButton  4:   5:  Private Sub Button_Click()  6:  MsgBox "Hello"  7:  End Sub  8:   9:  Private Sub Form_Load()  10:  Set Button = _  11:  Form1.Controls.Add("VB.CommandButton", "Dynamic")  12:  Button.Left = 100  13:  Button.Top = 100  14:  Button.Visible = True  15:  Button.Caption = "Dynamic"  16:  End Sub 

Lines 9 through 16 create and add the dynamic control in VB6. Line 11 both creates and adds a CommandButton using the library.controlname syntax, and returns the dynamic object to the Button variable. Lines 12 through 15 locate and name the control. A limitation here is wiring the control up to an event handler; the programmer must declare the WithEvents statement at design time. Requiring the WithEvents statement at design time limits dynamic control creation to some extent; this is necessary because there is no programmable equivalent to delegates in VB6.

Listing 16.8 Dynamic control creation in Visual Basic .NET
  1:  Private Sub Form1_Load(ByVal sender As System.Object, _  2:  ByVal e As System.EventArgs) Handles MyBase.Load  3:   4:  Dim Button As New Button()  5:  Button.Text = "Dynamic"  6:  Button.Location = New Point(10, _  7:  Controls.Count * Button.Height)  8:  AddHandler Button.Click, AddressOf Form1_Load  9:  Controls.Add(Button)  10:   11:  End Sub 

Listing 16.8 creates a new Button on line 4, and assigns the Button object to a variable named Button. The button's caption is added on line 5, and the Button.Location property is established on lines 6 and 7. Line 8 adds the event handler dynamically, and the Button is added to the form's Controls array on line 9.

When the dynamic control is added to the form's Controls collection on line 9, the Button control will receive messages, including a paint message to display the control.

Notice that you still have to have codein the case of the Button, an EventHandlerto respond to user inputs. Everything else about the code is completely dynamic.

The steps are identical for adding any controls to your applications at runtime. You can dynamically create as many controls as you need, all you have to do is ensure that you have somewhere to wire them to.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: