Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Catching Events of Dynamically Added Controls : Page 2

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.


advertisement

Lets get the big black bunny out of the hat. There is a Class Module missing here! Lets call this missing Class Module " CGral ". As the name suggests this module is useful for general purposes. One of the purposes may well be to raise some events. Sort of an event Publisher. We create this Class Module, set it’s instancing to MultiUse., and add a public Event to it:

evLabelClicked(ByVal Index As Integer)



and also a public method called gLabelClicked which raises the event evLabelClicked.

Public Sub gLabelCliked(ByVal Indx As Integer)

RaiseEvent evLabelClicked(Indx)

End Sub

Remember we had a place to handle the Label events in our CLabel Class Module. That is the place we use to call the CGral method gLableClicked. Of course we need the Index identifying the Label being clicked on, and we derive this Index from the Label Name Property. Here is one way to write this code:

Private Sub Lbl_Click()

Dim li_Len%

Dim li_Index%

li_Len = Len(mLbl.Name)

If li_Len = 6 Then li_Index = CInt(Right(mLbl.Name, 1))

If li_Len = 7 Then li_Index = CInt(Right(mLbl.Name, 2))

If li_Len = 8 Then li_Index = CInt(Right(mLbl.Name, 3))

' raises the event '

gGRAL.gLabelCliked (li_Index)

End Sub

So now we have Class Module CGral trumpeting the event within our application. But is our Form aware of it? Not yet. In the declaration area of our Form we must add

Private WithEvents mLabelSelected As CGral

And with this declaration VB will make a space available to us where we receive and handle the Lable_Click event. This is

Private Sub mLabelSelected_LabelClicked(ByVal Index As Integer)

Code to handle event here...

End Sub

 Now the last piece of the puzzle is in place.

Cleaning Up

Once you labels have appeared on your Form and you are through with clicking on them to your hearts content, its time to remove the Labels and clean up. Remember, the Labels "belong" to the Form on which they are drawn, that is, they are part of the Form.Controls collection. On the other hand, the artists that have drawn the Labels on your Form are the CLabel classes and they are independent of your Form. This code on your Form does it:

Private Sub ClearLabels()

Dim ctl As Control

Dim li%

' This removes the Labels

For Each ctl In Me.Controls

' use this Filter to remove only the Labels added dymanically

' you may have other Labels on tour Form you dont want removed

If Left(ctl.Name, 4) = "LblT" Then Me.Controls.Remove ctl.Name

Next

' Then you dismiss the artists

For li = mColTaskLength.Count To 1 Step -1

mColTaskLength.Remove li

Next

End Sub

You may not have yet noticed but we have barely escaped being caught in a "circular reference" problem. The trap was nicely set for us from the beginning, because your Form has a reference to the CLabel classes which in turn have a reference to the Form. How did we get away with this one? If you remember I highlighted one line of code as a very important line. That line read:

Set mCallingForm = Nothing

and occurred immediately after the line we used to draw our Labels on the Form. Once the Label is drawn we do not need the reference to the Form any more, so we destroy it at once, and avoid the "circular reference".

"A stitch in time...." applies here as well.



Christian Landesberger is an argentine industrial engineer, born in 1946, specialized in lighting and lighting fittings manufacturing. He did his first steps in programming using FORTRAN and punching paper ribbon for an IBM 1620, but he became really involved in programming with the appearance of VB 3.0. He is currently developing scheduling software for small and medium sized companies. Reach Christian at landesberger@fibertel.com.ar.
Comment and Contribute

 

 

 

 

 


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

 

 

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