How to detect the button on the UserControl is clicked from owner RRS feed

  • Question

  • There's a form(Form1) which have a UserControl.

    This UserControl has a button.

    The Form1 should detect the button on the UserControl is clicked.

    How to do this?

    Friday, June 28, 2019 2:53 AM

All replies

  • There are several ways.  You can make the button a public member, and then just let Form1 add an event handler for the button directly.  Or, you could expose a function from your user control that the form calls in order to register its event handler.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, June 28, 2019 4:02 AM
  • Try adding a new member to your User Control class:


    public event EventHandler MyButtonClicked;


    In User Control, handle the Click event of the button:


    private void Button1_Click( object sender, EventArgs e )


        MyButtonClicked?.Invoke( this, null );




    After recompilation and reopening the parent form, the MyButtonClicked event will appear in the list of events in Properties panel for the selected User Control. Then you can add a handler similarly to usual events. You can also use “+=” programmatically.

    Friday, June 28, 2019 4:50 AM