1

I have created a User Control and would like to be able to detect when the user clicks on the Form.

I have seen this question which is related but the suggestion to use the the Leave event doesn't always do what I want because the focus doesn't necessarily change when the user clicks the Form (my control could be the only control on the Form in which case focus stays with my control).

Any ideas?

I want to be able to do something like this from within the User Control:

Private Sub ParentForm_Click(sender As Object, e As System.EventArgs) _
    Handles Me.Parent.Click

End Sub
Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • So you're trying to figure out how to make events "bubble up" in WinForms, i.e., that the user can click on either the form or on its child control (your UserControl) and the `Click` event will be raised on the form? – Cody Gray - on strike Jan 12 '12 at 10:50
  • @Cody - No I want to be able to detect the Form click from within the User Control – Matt Wilko Jan 12 '12 at 10:55
  • I don't see how that could possibly make any sense. They won't be clicking the form because the user control is on top of it. And if they click *outside* of the user control, well then why should the user control ever get that information? They could have just as easily clicked on the Start menu. You won't expect it to get notified of *that* click event, would you? – Cody Gray - on strike Jan 12 '12 at 10:56
  • @Cody - related to [my other question](http://stackoverflow.com/questions/8822678/is-there-a-simple-way-to-implement-a-checked-combobox-in-winforms/8823336#8823336) When I have dropped down a selection of items, if the user clicks on the form I want to stop showing the dropped down list. This is how the control behaves in the trigger edit (and it also rolls up when you click on the Start menu btw) – Matt Wilko Jan 12 '12 at 11:16

2 Answers2

3

I would do it slightly differently:

Private _form As Control

Private Sub UserControl_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ParentChanged

    If _form IsNot Nothing Then
        RemoveHandler _form.Click, AddressOf ParentOnClick
    End If

    _form = Me.FindForm()

    If _form IsNot Nothing Then
        AddHandler _form.Click, AddressOf ParentOnClick
    End If

End Sub

Private Sub ParentOnClick(ByVal sender As Object, ByVal e As EventArgs)
    '...
End Sub

This gives it a little more resillience - if it is not a direct child of a Form, if its parent changes etc.

Pondidum
  • 11,457
  • 8
  • 50
  • 69
0

I figured out how to do this myself - for anyone interested I am doing the following:

Private _parentForm As Form

Private Sub UserControl_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    _parentForm = CType(Parent, Form)
    AddHandler _parentForm.Click, AddressOf ParentForm_Click
End Sub

Private Sub ParentForm_Click(sender As Object, e As System.EventArgs)
    debug.writeline("Parent form clicked")
End Sub
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143