Sharing common events

J

jcrouse

I am trying to share mouse events (click, up, down, move and the such) for
30 different label controls: Here is my code:



Dim myLabel as Label

Private Sub lblP1JoyUp_MouseDown(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lblP1JoyUp.MouseDown

myLabel = lblP1JoyUp

myLabelMouseDown()

End Sub





Public Sub myLabelMouseDown()

If e.Button = MouseButtons.Left Then

My code here

ElseIf e.Button = MouseButtons.Right Then

My code here

End If

End Sub


The problem is that it tells me "e" isn't declared. Am I missing something
with my handlers? I thought I had this working at one time with help from
Cor but can't find it no how.

Thanks,
John
 
J

jcrouse

I seem to have found Cor's example and a post from Jay. I'll give it another
go.

John
 
I

Imran Koradia

Private Sub lblP1JoyUp_MouseDown(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lblP1JoyUp.MouseDown

myLabel = lblP1JoyUp

myLabelMouseDown()

change this to: myLabelMouseDown(e)
Public Sub myLabelMouseDown()

and this to: Public Sub myLabelMouseDown(ByVal e as
System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then

My code here

ElseIf e.Button = MouseButtons.Right Then

My code here

End If

End Sub


The problem is that it tells me "e" isn't declared.

That's exactly what that means. In the procedure myLabelMouseDown 'e' hasn't
been declared anywhere.


hope this helps..
Imran.
 
H

Herfried K. Wagner [MVP]

* "jcrouse said:
I am trying to share mouse events (click, up, down, move and the such) for
30 different label controls: Here is my code:



Dim myLabel as Label

Private Sub lblP1JoyUp_MouseDown(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lblP1JoyUp.MouseDown

myLabel = lblP1JoyUp

myLabelMouseDown()

End Sub

Instead of adding one handler per control, add a common handler. To do
that, add all 30 controls to the 'Handles...' list at the procedure
head's end ('Handles Label1.MouseDown, Handles Label2.MouseDown, ...'),
or use 'AddHandler to add the common handler to all of the controls'
'MouseDown' event. Then you can check the event handler's 'sender'
parameter to see which control caused the event:

\\\
Dim SourceControl As Control = DirectCast(sender, Control)
....
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top