最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Handle event fired in User Control in main form - Stack Overflow

programmeradmin5浏览0评论

I have a User Control UC1 which consists of one button clickMeButton. I want to add this User Control to my main form Form1, and when I click on clickMeButton I want another button changeButton in the form to change text. However, nothing happens. I know there are several posts about this and I have read them but it doesn't solve my issue. Here is my code:

User control class:

public partial class UC1: UserControl
{
   public event EventHandler ClickHereButtonClicked;
   public UC1()
   {
    InitializeComponent();
   }

   private void OnButtonClick(object sender, EventArgs e)
   {
      ClickHereButtonClicked?.Invoke(this, e);
   }
}

Form:

public partial class Form1: Form
   {
   public Form1()
   {
      InitializeComponent();
      uC11.ClickHereButtonClicked += new EventHandler(ButtonClick_Handler);
   }

   private void ButtonClick_Handler(object sender, EventArgs e)
   {
      changeButton.Text = "Button clicked!";
    }
}

The User Control uC11 and changeButton are declared in the Form1.Designer class. I use Visual Studio.

I have a User Control UC1 which consists of one button clickMeButton. I want to add this User Control to my main form Form1, and when I click on clickMeButton I want another button changeButton in the form to change text. However, nothing happens. I know there are several posts about this and I have read them but it doesn't solve my issue. Here is my code:

User control class:

public partial class UC1: UserControl
{
   public event EventHandler ClickHereButtonClicked;
   public UC1()
   {
    InitializeComponent();
   }

   private void OnButtonClick(object sender, EventArgs e)
   {
      ClickHereButtonClicked?.Invoke(this, e);
   }
}

Form:

public partial class Form1: Form
   {
   public Form1()
   {
      InitializeComponent();
      uC11.ClickHereButtonClicked += new EventHandler(ButtonClick_Handler);
   }

   private void ButtonClick_Handler(object sender, EventArgs e)
   {
      changeButton.Text = "Button clicked!";
    }
}

The User Control uC11 and changeButton are declared in the Form1.Designer class. I use Visual Studio.

Share Improve this question asked Mar 21 at 15:42 Alexander JonssonAlexander Jonsson 18111 bronze badges 6
  • We can't check incomplete code if it's correct or not. Please refer to minimal reproducible example. Something is wrong in the chain. Set breakpoints and see if something is not called, I suspect OnButtonClick because it has unusual name for button event handler. – Sinatr Commented Mar 21 at 15:53
  • @Itallmakescents It didn't work – Alexander Jonsson Commented Mar 21 at 16:00
  • @Sinatr These 2 classes are the only ones I have entered code in. What more should I provide? – Alexander Jonsson Commented Mar 21 at 16:18
  • I don't see anything what is calling (by subscribing to click event?) OnButtonClick. To change text of changeButton someone should call OnButtonClick first. As I say previously the name is unusual, I suspect it's not event handler generated by designer, but maybe a custom method and noone is calling it. You can include designer-generated code (content of all *.Designer.cs files) to let us check it. – Sinatr Commented Mar 21 at 16:34
  • Could be some other problem. Set breakpoints and see if methods you expect to be called are really called. – Sinatr Commented Mar 21 at 16:35
 |  Show 1 more comment

1 Answer 1

Reset to default 0

As others have stated, it's possible that you don't have the OnButtonClick method wired up to the button in your usercontrol.

Change:

public UC1()
{
    InitializeComponent();
}

To:

public UC1()
{
    InitializeComponent();
    clickMeButton.Click += new EventHandler(OnButtonClick);
}
发布评论

评论列表(0)

  1. 暂无评论