Solved

RaiseEvent won't fire

Posted on 2002-03-13
27
924 Views
Last Modified: 2007-11-27
Hi all,

I am a bit puzzled as to why my RaiseEvent statement is not firing the appropriate event.

I have a class (e.g. clsFireEvent) which only purpose is to fire the event, i.e.

Public Event evLabelClicked(ByVal Index As Integer)

Public Sub gLabelClicked(ByVal Index As Integer)
    RaiseEvent evLabelClicked(Index)
End Sub

I have another class which calls the procedure (gLableClicked) above which raises the event.

However, when it goes through the RaiseEvent statement it does not go to the Event created in the form.

In the form I have declared the class with events, e.g.

Private WithEvents LabelSelected As clsFireEvent

And have placed code within this, but the RaiseEvent statement is not firing it.

Any ideas ?

Many thanks,

Christian
0
Comment
Question by:ChristianHaynes
  • 14
  • 10
  • 3
27 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6860151
and you have created LabelSelected_evLabelClicked procedure?
0
 

Author Comment

by:ChristianHaynes
ID: 6860188
Yes, I have a line of tempory code in there to place a break-point. But it never goes through it!
0
 

Author Comment

by:ChristianHaynes
ID: 6860191
Yes, I have a line of tempory code in there to place a break-point. But it never goes through it!
0
 

Author Comment

by:ChristianHaynes
ID: 6860193
Yes, I have a line of tempory code in there to place a break-point. But it never goes through it!
0
 

Author Comment

by:ChristianHaynes
ID: 6860194
Opps sorry!!
0
 
LVL 13

Expert Comment

by:crazyman
ID: 6860290
and have you then
 
set LabelSelected = new clsFireEvent
LabelSelected.gLabelClicked(0)

?
0
 

Author Comment

by:ChristianHaynes
ID: 6860349
crazyman,

I have the code:

LabelSelected.gLabelClicked(0) - This works fine without the set statement.

LabelSelected is of type Label encapsulated within a class, and when I try to use the statement:

set LabelSelected = new clsFireEvent

I get a type mismatch error because surely I cannot reference a label against a class of a totally different structure!


0
 
LVL 13

Expert Comment

by:crazyman
ID: 6860386
huh you say it is of type label yet you have this??

Private WithEvents LabelSelected As clsFireEvent


basically when declaring an object withevents you then need to set this to the object for which you wish to handle events for, i think this is where you have gone wrong.

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6860409
as long as you will not have an instance (using NEW) you won't get events for sure.

can you copy more complete code so that we can see where your error could be?
0
 

Author Comment

by:ChristianHaynes
ID: 6860461
Here is the code and what it is supposed to do:

When a label is clicked, the click event within the clsLabel class is fired, see below:

'General Declaration
Private clsFireEvent As New cFireEvent

Private Sub iLbl_Click(Index As Integer)
   
    Dim li_Index As Integer
           
   'raises the event
    clsFireEvent.gLabelClicked (Index)
   
End Sub

This works fine and calls the FireEvent Class (cFireEvent), class code below:

Public Event evLabelClicked(ByVal Index As Integer)

Public Sub gLabelClicked(ByVal Index As Integer)
    RaiseEvent evLabelClicked(Index)
End Sub

It then gets to the RaiseEvent statement and passes through it without raising the event setup in the form.. The form code is setup as follows:

'General Declarations
Private WithEvents LabelSelected As cFireEvent

Which gives me the code with event:

Private Sub LabelSelected_evLabelClicked(ByVal Index As Integer)
    This is not firing!!!
    Dim test
    test = test
End Sub

So when the event is raised in cFireEvent I thought it would fire this event..

What am I missing ?

Hope this is clearer.

Thanks.

Christian
0
 
LVL 13

Expert Comment

by:crazyman
ID: 6860472
of course it wont fire

you have
Private clsFireEvent As New cFireEvent
clsFireEvent.gLabelClicked (Index)


yet your trying to marshall events through
Private WithEvents LabelSelected As cFireEvent

LabelSelected is probably equal to nothing


You could do

Private clsFireEvent As New cFireEvent
set LabelSelected = clsFireEvent
clsFireEvent.gLabelClicked (Index)
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6860497
I have modified your code completly. See it now working:

The clsLabel class:

Option Explicit

Public WithEvents iLbl As Label

Public Event evLabelClicked(ByVal Index As Integer)

Private Sub iLbl_Click()
  'raises the event
   RaiseEvent evLabelClicked(0)
End Sub


The form:

Option Explicit

'General Declarations
Private WithEvents LabelSelected As clsLabel

Private Sub Form_Load()
    Set LabelSelected = New clsLabel
    Set LabelSelected.iLbl = Label1
End Sub

Private Sub LabelSelected_evLabelClicked(ByVal Index As Integer)
   Stop
End Sub

0
 

Author Comment

by:ChristianHaynes
ID: 6860629
emoreau,

I don't get the line:

Set LabelSelected.iLbl = Label1

I cannot execute the above line until I have a reference of a label and that will only happen when I click on one, and you have it in the form load event where there would be no label reference at this point.

And when the label is clicked it executes the event iLbl_Click in clsLabel first which contains the RaiseEvent statement.

Thanks
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6860750
to have the clsLabel intercepting the events of a control, you need to pass your instance to it. If you change Label1 to your actual control name, it will work.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6860756
>>form load event where there would be no label reference at this point.

why would you generate the click event of a control that is not loaded yet?
0
 

Author Comment

by:ChristianHaynes
ID: 6860772
emoreau,

The labels are created dynamically at runtime.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6860786
>>The labels are created dynamically at runtime.

if you are using a control array, you can't subclass these controls (you can't pass label1(0) to your iLbl).
0
 

Author Comment

by:ChristianHaynes
ID: 6860799
Im not using a control array, the control names contain a number and that is incremented each time a Label is created.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6860832
ok so just after you created your new label, you will be able to pass its instance to your class.

you will need to create an array of LabelSelected.
0
 

Author Comment

by:ChristianHaynes
ID: 6860898
Each Label is actually created within the cLabel Class.

So your saying I now have to create a array within the cLabel class ?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6860927
cLabel instance MUST be binded to an instance on the form. the form must pass its instance to the class otherwise both objects won't be binded together.
0
 

Author Comment

by:ChristianHaynes
ID: 6861167
emoreau,

OK, I'm starting to get what you are saying but can't seem to apply a working version, here's the code within the Form and Label class, (It just seems pointless binding the label in the class to the form and then rebinding back to the class).. I'm confused:


'Form
Private ilblNewLabel As clsLabel
Private WithEvents iclsLabelSelected As clsLabel

'Instatiates a new label
Set ilblNewLabel = New cLabel

'Sets the Form that the label is to be inserted onto
ilblNewLabel.CallingForm = Me

'Draws the label on the form
ilblNewLabel.gDrawOnForm "LblT" & intX, Source.Text, Y, X



'cLabel Class
Public WithEvents iLbl As Label
Private iCallingForm As Form
Public Event evLabelClicked(ByVal Index As Integer)

Public Property Let CallingForm(frm As Object)
   Set iCallingForm = frm
End Property

Public Sub gDrawOnForm(ByVal ControlName$, ByVal FieldName$, ByVal Top%, ByVal Left%)

'Creates the label from the Controls collection of the form    
Set iLbl = iCallingForm.Controls.Add _("VB.Label",ControlName, iCallingPicBox)
    Set iCallingForm = Nothing

    'Set Label properties
    iLbl.AutoSize = False
    iLbl.BackStyle = 1
    iLbl.Top = Top
    iLbl.Left = Left
    iLbl.BackColor = &H80000005
    iLbl.Caption = FieldName
    iLbl.Enabled = True
    iLbl.Visible = True

End Sub

Private Sub iLbl_Click()
    'Label click event
    Dim li_Len As Integer
    Dim li_Index As Integer
   
    li_Len = Len(iLbl.Name)
   
    If li_Len = 5 Then li_Index = CInt(Right(iLbl.Name, 1))
    If li_Len = 6 Then li_Index = CInt(Right(iLbl.Name, 2))
    If li_Len = 7 Then li_Index = CInt(Right(iLbl.Name, 3))
    If li_Len = 8 Then li_Index = CInt(Right(iLbl.Name, 4))
       
   'raises the event
    RaiseEvent evLabelClicked(li_Index)
   
End Sub
0
 

Author Comment

by:ChristianHaynes
ID: 6861232
emoreau,

In the form after the line:

ilblNewLabel.gDrawOnForm "LblT" & intX, Source.Text, Y, X

If I do:

'General Declaration
Private WithEvents iiclsLabelSelected cLabel

Set iclsLabelSelected.iLbl = ilblNewLabel.iLbl

It fires the event fine for the last label created (which is great progress), however, if there are multiple labels and a label (other than the last one created) is clicked it does not fire because the instance on the form is still bound to the last one created..

Is this where I need to create an array and then loop through it to find the correct label clicked and then bind it as the current label ?

Thanks



0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 6861470
you won't be able to have everything you want with VB6 (but VB.Net can):

'Form
Option Explicit

Private ilblNewLabel(1 To 3) As cLabel
'Private WithEvents iclsLabelSelected As cLabel
'Private WithEvents iiclsLabelSelected As cLabel


Private Sub Command1_Click()
Dim intX As Integer

    For intX = 1 To 3
    'Instatiates a new label
    Set ilblNewLabel(intX) = New cLabel
   
    'Sets the Form that the label is to be inserted onto
    Set ilblNewLabel(intX).CallingForm = Me
   
    'Draws the label on the form
    ilblNewLabel(intX).gDrawOnForm "LblT" & intX, "LblT" & intX, 500 * intX, 120
    Next
'Set iclsLabelSelected.iLbl = ilblNewLabel.iLbl
End Sub

Public Sub CallMe(ByVal pintI As Integer)
    MsgBox pintI
End Sub



'Class

Option Explicit

Public WithEvents iLbl As Label
Private iCallingForm As Form
'Public Event evLabelClicked(ByVal Index As Integer)

Public Property Set CallingForm(ByRef frm As Form)
  Set iCallingForm = frm
End Property

Public Sub gDrawOnForm(ByVal ControlName$, ByVal FieldName$, ByVal Top%, ByVal Left%)

'Creates the label from the Controls collection of the form
Set iLbl = iCallingForm.Controls.Add("VB.Label", ControlName)  ', iCallingPicBox)
'   Set iCallingForm = Nothing

   'Set Label properties
   iLbl.AutoSize = False
   iLbl.BackStyle = 1
   iLbl.Top = Top
   iLbl.Left = Left
   iLbl.BackColor = &H80000005
   iLbl.Caption = FieldName
   iLbl.Enabled = True
   iLbl.Visible = True

End Sub

Private Sub iLbl_Click()
   'Label click event
   Dim li_Len As Integer
   Dim li_Index As Integer
   
   li_Len = Len(iLbl.Name)
   
   If li_Len = 5 Then li_Index = CInt(Right(iLbl.Name, 1))
   If li_Len = 6 Then li_Index = CInt(Right(iLbl.Name, 2))
   If li_Len = 7 Then li_Index = CInt(Right(iLbl.Name, 3))
   If li_Len = 8 Then li_Index = CInt(Right(iLbl.Name, 4))
     
  'raises the event
'   RaiseEvent evLabelClicked(li_Index)
   Call iCallingForm.CallMe(li_Index)
End Sub
0
 

Author Comment

by:ChristianHaynes
ID: 6864103
emoreau,

Is this VB.Net code ?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6864338
this is VB6 code.
0
 

Author Comment

by:ChristianHaynes
ID: 6868402
emoreau,

I think we're just about there, just a bit of tweaking here and there.

Many thanks,

Christian

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now