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:
Index As Integer)
and also a public
method called gLabelClicked which raises the event evLabelClicked.
gLabelCliked(ByVal Indx As Integer)
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()
li_Len = Len(mLbl.Name)
If li_Len = 6 Then li_Index =
If li_Len = 7 Then li_Index =
If li_Len = 8 Then li_Index =
raises the event '
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
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
mLabelSelected_LabelClicked(ByVal Index As Integer)
to handle event here...
last piece of the puzzle is in place.
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
' This removes
For Each ctl In
' 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
' Then you
dismiss the artists
li = mColTaskLength.Count To 1 Step -1
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:
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.