Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Catching Events of Dynamically Added Controls

VB now allows us to add controls to a Form at run time. This article explains a way in which you can dynamically add a control array to a Form at run time and then catch the events generated by these added controls. This isn't possible, unless you use some auxialiry classes and some tricks.


VB now allows us to add controls to a Form at run time. In this article I will try to explain a way in which you can dynamically add a control array to a Form at run time and then catch the events generated by these added controls.

Basic Concepts

Suppose we have a Form, named FrmTest to which we want to add a Label Control at run time. We must make our intention clear from the beginning, giving the Label Control a name, and letting FrmTest know about it. Lets call this Label Control mLblTaskLength, because with it we intend to display on FrmTest the time a certain manufacturing operation takes. Therefore, in the declaration area of FrmTest we add:

Private WithEvents mLblTaskLength As Label

We use the prefix "m" as a convention to help us identify a variable which scope is only within FrmTest. Having declared this variable WithEvents, we now find it displayed in the left dropdown list of our code window, where we can write code to handle the various events the Label Controls can generate. When the need to show the label arises, we use the following code to make it appear on our Form:

Set mLblTaskLaskLength = Me.Controls.Add("VB.Label","mLblTaskLength")

The first parameter "VB.Label" or ProgID identifies the Label Control, the second parameter is the controls name.

This code will create the Label control for us, although it shall not be immediately visible and still VB is not aware of the new Label property values. Therefore we continue with our code, setting the Property values and telling VB where to display this new control:

With mLblTaskLaskLength

.Appearance = 1

.AutoSize = False

.BackStyle = 1 'Opaque

.BorderStyle = 0 'None


End With

We can use the "move" method to place the control on the Form.

mLblTaskLaskLength.move 500,500,100,800

Were the parameters indicate: Top, Left, Height and Width properties of the Label. Finally we make the Label visible.

mLblTaskLaskLength.Visible = True

After this code is executed the Label will display nicely on our Form. This covers the basics.

Adding an Array of Controls

Now let us venture forward. We want to create an array of Labels, because we have many manufacturing tasks, the duration of which, we want to display with Labels. We soon discover that VB does not allow us to display an array of controls in the manner we have just learnt. We need a workaround for this. One way to achieve this goal is to encapsulate a Label Control inside a Class Module. We then add to our Project a new Class Module and name it CLabel. We set its instancing property to Private. In Clabel declaration area we add the following:

Option Explicit

Private WithEvents mLbl As Label

Private mCallingForm As Form

Let us take a closer look at what we have declared. With mLabel we are creating a variable to hold the Label Control encapsulated in our Class Module. With mCallingForm we have a variable to hold a reference to the Form on which we want the Label displayed. Please note we have declared mLbl WithEvents. As in the previous case, you shall find in the left Dropdown list of your code Window, a place made available to handle the events mLbl generates. This is of course a minimum set of declarations. When on your own, you can declare other variables, one for Caption, another for BackStyle...

Now we need to add the following property to our Class Module:

Public Property Let CallingForm(frm As Form)

Set mCallingForm = frm

End Property

We then need a method to call and have it draw the Label on the Form for us. Lets call this method gDrawOnForm. Here is how to do it:

Public Sub gDrawOnForm(ByVal Name$,ByVal Top%,ByVal Left%,ByVal Height%, ByVal Width%)

Set mLbl = mCallingForm.Controls.Add("VB.Label", fs_Name)

Set mCallingForm = Nothing 'Important Line of Code

' we set the properties '

mLbl.Autosize = false

mLbl.BackStyle =1


'then the position '

mLbl.Top = Top

mLbl.Left = Left

mLbl.Height= Height

mLbl.Width = Width

mLbl.Visible = True

End Sub

You can add other parameters to the List, as you see fit.

Now lets go back to our Form, on which we want the array of Labels displayed. On the declaration area we declare the variable to hold the array and add a Collection to hold the array of Labels.

Private mLblTaskLength As Clabel

Private mColTaskLength As Collection

When we are ready to create the array we do it in this manner:

Dim li%

Set mColTaskLength = New Collection

For li = 0 To NumberOf Labels

Set mLblTaskLength = New Clabel

' Add Item to Collection '

mColTaskLength.Add mLblTaskLength

mLblTaskLength.gDrawOnForm "LblT" & li, other parameters

Set mLblTaskLength = Nothing


Notice how we have named our Labels. This is important. They are named LblT0, LblT1, and LblT2... Once this code executes you shall find the Labels displayed on the Form. By now you might be thinking, "Wait.......... you started talking about catching events. Where are the events now? You didn’t even declare mLblTaskLenght WithEvents. You see, I still have some bunnies in my hat, I am not through yet.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date