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.
这篇关于如何将多个按钮订阅到同一个事件处理程序并根据单击的按钮进行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将多个按钮订阅到同一个事件处理程序并根据单击的按钮进行操作?
基础教程推荐
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 将数据集转换为列表 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 如果条件可以为空 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
