Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 941
  • Last Modified:

RaiseEvent won't fire

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
ChristianHaynes
Asked:
ChristianHaynes
  • 14
  • 10
  • 3
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
and you have created LabelSelected_evLabelClicked procedure?
0
 
ChristianHaynesAuthor Commented:
Yes, I have a line of tempory code in there to place a break-point. But it never goes through it!
0
 
ChristianHaynesAuthor Commented:
Yes, I have a line of tempory code in there to place a break-point. But it never goes through it!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ChristianHaynesAuthor Commented:
Yes, I have a line of tempory code in there to place a break-point. But it never goes through it!
0
 
ChristianHaynesAuthor Commented:
Opps sorry!!
0
 
crazymanCommented:
and have you then
 
set LabelSelected = new clsFireEvent
LabelSelected.gLabelClicked(0)

?
0
 
ChristianHaynesAuthor Commented:
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
 
crazymanCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
ChristianHaynesAuthor Commented:
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
 
crazymanCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
ChristianHaynesAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
>>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
 
ChristianHaynesAuthor Commented:
emoreau,

The labels are created dynamically at runtime.
0
 
Éric MoreauSenior .Net ConsultantCommented:
>>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
 
ChristianHaynesAuthor Commented:
Im not using a control array, the control names contain a number and that is incremented each time a Label is created.
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
ChristianHaynesAuthor Commented:
Each Label is actually created within the cLabel Class.

So your saying I now have to create a array within the cLabel class ?
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
ChristianHaynesAuthor Commented:
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
 
ChristianHaynesAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
ChristianHaynesAuthor Commented:
emoreau,

Is this VB.Net code ?
0
 
Éric MoreauSenior .Net ConsultantCommented:
this is VB6 code.
0
 
ChristianHaynesAuthor Commented:
emoreau,

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

Many thanks,

Christian

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 14
  • 10
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now