Click Event

jimbo2k

Newcomer
Joined
Mar 31, 2004
Messages
16
Location
ON,Canada
I have a panel, and inside theres many labels and pictureboxes.

Basically, I want to be able to catch the users click event whether its on a label, or the panel, or anything else and do something.

I am aware of how to catch a click event on a particular label/picturebox, but there has to be a better way to do this.

Thanks.
 
You could make a generic click event handler function and dynamically add the handlers.

Here, reusable code:
Visual Basic:
'This is our event handler
Private Sub Generic_Click(ByVal sender As Object, ByVal e As EventArgs)
    MessageBox.Show("I been clicked")
End Sub

'This function will add the specified handler to a control and any controls it contains
Private Sub AddGenericClickHandler(ByVal Parent As Control, ByVal Handler As EventHandler)
    For Each thing As Control In Parent.Controls
        AddHandler thing.Click, Handler
    Next
    AddHandler Parent.Click, Handler
End Sub

Just put this line in your Sub New() or constructor:
AddGenericHandler(Panel1, AddressOf Generic_Click)
 
Wow, Brilliant. Just curious, Im guessing this is an initialization statement which must be put in the New() sub?

On a side note, would you happen to be familiar with sockets ?

Thanks a alot
 
Not at all familiar with sockets. Played around with winsock a bit in vb6, but never quite figured it out.

And you can add handlers any time you want for any controls and any handlers you want, which is useful for, say, dynamically adding controls to your form and being able to handle their events. Look up the details on the AddHandler statement if you want to learn more.
 
marble_eater said:
Not at all familiar with sockets. Played around with winsock a bit in vb6, but never quite figured it out.

Well, if you (or anyone that reads this) ever want an introductory to the basics (being able to send a string of data across the network to an endpoint) then I'd be happy to teach.
 
Private Sub Allbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click, btn2.click
 
MarbleEater, im back :-\

Ive realized I dont want a generic click, but instead, a generic "mouse down" event.

Ive tried :

Code:
Private Sub AddGenericClickHandler(ByVal Parent As Control, ByVal Handler As EventHandler)
        For Each thing As Control In Parent.Controls
            AddHandler thing.MouseDown, Handler
        Next
        AddHandler Parent.MouseDown, Handler

but needless to say, it doesnt like that. Ive also tried replacing the "Handler As EventHandler" in the arguements list, to "...AS MouseEventHandler" but it gives me a "does not have the same signature" error.

Any insight? Thanks
 
To change which event you are handling here you need to change a few things:
-The signature of the generic handler must be correct. In the case of mousedown it would be Sub(Object, MouseEventArgs).
-You must use the correct delegate for the handler adding sub. In the case of mousedown it would be MouseEventHandler
-You must change the actual event being handled
Visual Basic:
'This is our event handler
Private Sub Generic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) '<--Need correct signature
    'Place your code here
End Sub

'This function will add the specified handler to a control and any controls it contains
Private Sub AddGenericClickHandler(ByVal Parent As Control, ByVal Handler As MouseEventHandler) '<--Handler is out delegate
    For Each thing As Control In Parent.Controls
        AddHandler thing.MouseDown, Handler '<--Actual Event
    Next
    AddHandler Parent.MouseDown, Handler '<--Actual Event
End Sub
 
The sender argument passes the object that raises an event as type Object. You can cast to a control and check it's name property, or compare it against a known variable...
Visual Basic:
If Sender Is PictureBox1 Then
    PictureBox1.Image = LoadSomeBitmap()
End If
You could store an index or key in the Tag property...
Visual Basic:
Dim Index As Integer = Integer.Parse(DirectCast(Sender, Control).Tag.ToString())
There are lots of ways.
 
Back
Top