Call a button on a C# form through usercontrol

后端 未结 4 2207
广开言路
广开言路 2021-01-23 18:37

I\'ve created a user control with some buttons. When you click a button in the UserControl the BackColor of the button changes:

 private void button1(object send         


        
4条回答
  •  灰色年华
    2021-01-23 19:04

    In your UserControl make event handler ColorChanged and fire that event when color changes. In your form add listener and appropriate code when event fires.

    So, in your control, make EventHandler, like this

    public partial class UserControl1 : UserControl
    {
        public EventHandler ColorChanged; 
    

    then, fire event on your button click, like this:

    private void button1(object sender, EventArgs e)
    {
        ColorChanged?.Invoke(sender, e);
        //rest of your code...
    }
    

    in your form, add listener:

    userControl.ColorChanged += new EventHandler(UserControl_ColorChanged)
    

    and add method that will be executed and enable button...

    private void UserControl_ColorChanged(object sender, EventArgs e)
    {
        //enable button here
    }
    

提交回复
热议问题