what's the best way to detect click in child controls?

B

Bob

If I have ControlX that contains ControlY, and the user clicks on ControlY,
I want to catch that in ControlX. I know I could use the OnControlAdded
override to add a click event handler to every control added to ControlX,
but that only works one level deep unless I recurse through all the children
and also handle every child's ControlAdded event, which is kind of ugly. Is
there a better way?

TIA,
Bob
 
B

Bob

Hmmm, this seems to work, though I'm told that overriding WndProc hurts
performance.

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If m.Msg = 33 Then Beep()
End Sub
 
G

Guest

Why not create a static class

public static class SharedEvent

public static event EventHandler ControlClicked

public static void FireClicked(object clickedControl

if (ControlClicked != null) ControlClicked(clickedControl, null)



Then have the parent control subscribe to the event and the child controls call FireClicked from their own click handlers
I don't know how specific you need to be in telling the parent which child was clicked, but it would work in a generic situation.
 

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