获取在控件上执行的事件

发布于 2024-12-07 02:17:01 字数 183 浏览 0 评论 0原文

我希望得到一些帮助。

在我的应用程序中,我有一个文本框,它应该显示单击任何控件时发生的事件。

例如,如果我单击应用程序中的按钮或复选框,则文本框应识别该事件并显示为“button1.clicked”、“checkbox1.checked”之类的内容。

谁能帮我完成这件事。

提前致谢。:)

I am hoping for some help.

In my application, I have a Text Box which should display the event occured in any control while clicking it.

For exmaple, if I click on a button or a checkbox in the app., the text box should identify the event and display as button1.clicked, checkbox1.checked kind of things.

Can anyone help me to get this done.

Thanks In Advance.:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-12-14 02:17:01

如果您想动态地为所有控件执行此操作,您可以浏览控件树并为如下所示的事件订阅相同的事件处理程序,在预定义控件集的情况下,基本上只需为相同的事件处理程序订阅所有控件。

预定义控件集

checkBox1.CheckedChanged += (s, e) => { /*  handles Checked state changes*/ };
button1.Click += (s, e) => { /*  handles button click */ };

动态

private void InitializeHooks()
{
    foreach(var control in this.Controls)
    {
       var button = control as Button;
       if (button != null)
       {
          button.Click += OnClick;
          continue;
       }

       var checkBox = control as CheckBox;
       if (checkBox != null)
       {
          checkBox.CheckedChanged += OnCheckedChanged;
       }       
    }
}

// Button click handler
private void OnClick(object sender, EventArgs eventArgs)
{
   txtOutput.Text = String.Format(
                                CultureInfo.CurrentCulture,
                                "{Control Type: {0}, Name: {1}, Event: Click",
                                sender.GetType().Name,
                                sender.Id);
}

// Checkbox Checked state changed handler
private void OnCheckedChanged(object sender, EventArgs eventArgs)
{
     txtOutput.Text = String.Format(
                                CultureInfo.CurrentCulture,
                                "{Control Type: {0}, Name: {1}, Event: CheckedChanged",
                                sender.GetType().Name,
                                sender.Id);

}

If you want to do it for all controls dynamically you can navigate through the controls tree and subscribe the same event handler for events like shown below, in case of predefined controls set basically just subscribe all of them for the same even handlers.

Predefined controls set

checkBox1.CheckedChanged += (s, e) => { /*  handles Checked state changes*/ };
button1.Click += (s, e) => { /*  handles button click */ };

Dynamic

private void InitializeHooks()
{
    foreach(var control in this.Controls)
    {
       var button = control as Button;
       if (button != null)
       {
          button.Click += OnClick;
          continue;
       }

       var checkBox = control as CheckBox;
       if (checkBox != null)
       {
          checkBox.CheckedChanged += OnCheckedChanged;
       }       
    }
}

// Button click handler
private void OnClick(object sender, EventArgs eventArgs)
{
   txtOutput.Text = String.Format(
                                CultureInfo.CurrentCulture,
                                "{Control Type: {0}, Name: {1}, Event: Click",
                                sender.GetType().Name,
                                sender.Id);
}

// Checkbox Checked state changed handler
private void OnCheckedChanged(object sender, EventArgs eventArgs)
{
     txtOutput.Text = String.Format(
                                CultureInfo.CurrentCulture,
                                "{Control Type: {0}, Name: {1}, Event: CheckedChanged",
                                sender.GetType().Name,
                                sender.Id);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文