How can I subscribe multiple buttons to the same event handler and act according to what button was clicked?(如何将多个按钮订阅到同一个事件处理程序并根据单击的按钮进行操作?)
问题描述
我有 6 个按钮要附加到同一个处理程序.我该怎么做?
I have 6 buttons that I want to be attached to the same handler. How can I do this?
推荐答案
当您订阅按钮上的事件时,它只是一个标准的事件处理程序:
When you subscribe to the event on a button, it's just a standard event handler:
button1.Click += myEventHandler;
您可以使用相同的代码为每个按钮添加处理程序:
You can use the same code to add handlers for every button:
button1.Click += myEventHandler;
button2.Click += myEventHandler;
button3.Click += myEventHandler;
button4.Click += myEventHandler;
button5.Click += myEventHandler;
button6.Click += myEventHandler;
这将导致您在 myEventHandler
中的处理程序在任何按钮被点击时运行.
This will cause your handler in myEventHandler
to be run when any of the buttons are clicked.
这篇关于如何将多个按钮订阅到同一个事件处理程序并根据单击的按钮进行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将多个按钮订阅到同一个事件处理程序并根据单击的按钮进行操作?


- 带问号的 nvarchar 列结果 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 使用 rss + c# 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01