如何识别单击了哪个按钮 [英] How to identify which button was clicked

查看:65
本文介绍了如何识别单击了哪个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



i正在用AddHandler做一些实验,只需几个按钮



的例子

如果我已经在一个表单中创建了2个按钮

如何确保ii点击第一个按钮它会弹出一个msgBox(按钮1)和按钮对于msgBox(按钮2)



如何识别哪个按钮被点击?



我尝试了什么:



for i as integer = 0 to 1



dim _button as new button



with _button







结束



next



AddHandler _button.Click,_buttonClicked的地址





private sub _buttonClicked(byVal _sender as object,byVal _e as EventArgs){

'if button 1点击

'MsgBox(按钮1)



'如果点击按钮2

' MsgBox(按钮2)

}

解决方案

这很可能,但这是一个坏主意。使用这种识别的代码将是不稳定的,难以维护。这不是面向事件的编程的工作原理。



一个问题是:你没有标记你正在使用的UI框架/库。但是,按钮在所有按钮中的处理方式非常相似。通常,添加到 Click 事件实例的调用列表中的委托签名有两个参数: object sender 和另一个,一个事件参数类型。



可以使用与几个按钮的事件处理程序相同的方法。有时候,它可以做出很好的设计。显然,这就是你所做的,否则你不需要识别任何东西。现在, sender 始终是对其中一个按钮对象的引用。因此,您可以执行一组检查:如果sender = myFirstButton则...如果sender = mySecondButton则... 但这将是滥用。



一般方法是为每个按钮添加一个单独的事件处理程序。在这种方法中,处理程序最好是匿名函数。我通常建议不要通过设计师添加任何事件处理程序;目前,设计师创建了难以维护的过时代码(如果WPF,情况有点不同,但我仍然建议小心使用XAML方法),但这完全取决于你。



正如我所说,使用相同的偶数句柄可能很有用,但只有当几个按钮做几乎相同的事情时。经验法则是:每当你需要识别一个按钮时,你做错了。正确的方法是:首先,想一想为什么要使用相同的处理程序?答案永远是:因为代码非常相似。现在问问自己,有什么区别?当你弄清楚这一点时,你的问题就减少了另一个问题:传递这个不同的项目。您可以使用按钮标签,但一般的解决方案是,例如,带按钮键的字典,以及在处理程序逻辑中使用的值。希望你能掌握这个想法。



-SA


你可以获得Button控件和它的细节很容易。



参考 - 获取按钮的ID /名称在点击事件中。 VB.NET - Stack Overflow [ ^ ]

hello,

i was doing some experiment on AddHandler with a few button

for an example
if i already create 2 buttons in a form
how to make sure that i i click on the 1st button it will pop out a msgBox("button 1") and button for a msgBox("button 2")

how to indentify which button was click??

What I have tried:

for i as integer = 0 to 1

dim _button as new button

with _button



end with

next

AddHandler _button.Click , address of _buttonClicked


private sub _buttonClicked ( byVal _sender as object , byVal _e as EventArgs){
'if button 1 was clicked
'MsgBox("Button 1")

'if button 2 was clicked
'MsgBox("Button 2")
}

解决方案

It's quite possible, but this is a bad idea. The code using such identification would be unstable, hard to maintain. This is not how event-oriented programming works.

One problem is: you did not tag the UI framework/library you are using. However, buttons are handled very similarly in all of them. Quite generally, the signature of the delegate added to the invocation list of the Click event instance takes two parameters: object sender and another one, of one of the event argument types.

It's possible to use the same method as the event handler for several buttons. Sometimes, it could make a good design. Apparently, this is what you do, otherwise you would not need to identify anything. Now, sender is always a reference to one of the button objects. So you can perform a set of checks: if sender = myFirstButton then… if sender = mySecondButton then… But this would be the abuse.

The general approach is to add a separate event handler to each button. In this approach, the handler should better be an anonymous function. I usually advice not adding any event handlers via the designer; presently, designers create obsolete code which is harder to maintain (if WPF, the situation is a bit different, but I still would recommend to be careful with XAML approaches), but it's really up to you.

As I say, using the same even handle can be useful, but only if several buttons do "almost the same thing". The rule of thumb is: whenever you need to "identify a button", you do it wrong. The right approach is: first, think why do you want to use the same handler? The answer will always be: because the code would be very similar. Now ask yourself, what would be the difference? When you figure this out, your problem is reduced to another problem: passing this "different item". You can use button tags, but the general solution would be, for example, the dictionary with button keys, and the values use in the handler logic. Hope you can grasp the idea.

—SA


You can get the Button control and its details easily.

Refer - Get the ID/Name of a button in a click event. VB.NET - Stack Overflow[^]


这篇关于如何识别单击了哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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