如何将多个按钮订阅到同一个事件处理程序并根据单击的按钮进行操作? [英] How can I subscribe multiple buttons to the same event handler and act according to what button was clicked?

查看:10
本文介绍了如何将多个按钮订阅到同一个事件处理程序并根据单击的按钮进行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 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.

这篇关于如何将多个按钮订阅到同一个事件处理程序并根据单击的按钮进行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆