UCL Event Handling Problem

pfullarton

New member
Joined
Aug 22, 2012
Messages
3
Programming Experience
10+
Hi Guys,

Just joined so apols if I get this wrong.....

I'm building an application that dynamically adds a variable number of a single user control into a panel and this is all working fine. Now, I'm trying to add an event handler to manage a click event for these controls.

First off is:

AddHandler CType(Nc, uclComp).MouseClick, AddressOf uclComp_Click

I then have this:

Public Sub uclComp_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Nc.MouseClick

If e.Button = MouseButtons.Left Then MessageBox.Show(Nc.MachineName)

End Sub

The code all compiles and the ADD HANDLER code is executed fine. However, when I click any of the dynamically created controls uclComp_Click is NOT firing.

Any ideas?

Thx in advance.

Peter
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
First you need to get rid of that Handles clause on the event handler. Inside the event handler, you use the 'sender' parameter to access the object that raised the event. That's the one and only reason that that parameter exists.

As for the event not being raised, are you actually clicking on the surface of the UC itself or are you clicking on a child control inside it? If it's the latter then that's your explanation. It's that child that will be raising the MouseClick event, not the UC.
 

pfullarton

New member
Joined
Aug 22, 2012
Messages
3
Programming Experience
10+
Thx for that - Worked. After removing the extraneous HANDLER, I found that the click was for controls within the UCL and not the UCL itself, as you quite rightly highlighted.

Now a really dumb question - How to I address the UCLs within the panel - I guess that this is an array/collection of UCLs. I'm always getting data returned on the last item and not the one actually clicked.
 
Top Bottom