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

  • Thread starter Thread starter Bob
  • Start date Start date
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
 
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
 
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.
 
Back
Top