关于Click事件[VB .NET] [英] About Click event [VB .NET]

查看:94
本文介绍了关于Click事件[VB .NET]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法处理1 sub中的所有点击事件?我有20个按钮。

解决方案

是的。它应该是这样的:

  foreach (按钮按钮 in  allButtons){
button.Click + =(sender,eventArgs){
ButtonClickHandler((Button)sender); // 注意输入类型:
// 发件人保证有一个运行时类型Button
};
}

void ButtonClickHandler(按钮按钮){
// 做点什么
}





但是现在,问题是:好的,你可以用这个或其他方式为几个按钮使用一个处理程序。但是你真的需要做同样的事情来回应点击这些按钮吗?几乎不。我宁愿假设你想做一些不同的事情。



这是你想要做的事情,但不应该

  void  ButtonClickHandler(按钮按钮){
if (button == myFirstButton) // 丑陋!
DoOneThing(); // 丑陋!
else if (button == mySeconfButton) // 丑陋!
DoSomethineElse(); // 丑陋!
// ...甚至更丑!
}





你能否看到它完全违背了统一处理程序方法的目的?它可能比为每个按钮添加单独的处理程序更糟糕。一个常见的处理程序只有在处理它对所有按钮完全相同时才有用,只有 data 是不同的。但是如何传递这些数据呢?在大多数情况下,只传递数组中按钮的索引或类似的东西就足够了。这取决于按钮的用途。



这样的事情:

  for  int  index =  0 ; index <  allButtons.Length; ++ index){
allButtons [index] .Click + =(sender,eventArgs){
ButtonClickHandler(index); // 这里使用的是一个非常重要的功能:闭包
};
}

void ButtonClickHandler( int index){
// 使用index作为某些数据数组/集合的参数做一些事情
}





你明白了吗?



完成关键任务回顾使用公共处理程序的想法,我需要注意:使用20个按钮的整个想法是值得怀疑的,这是问题的根源。为什么?你可以考虑一个列表框和一个按钮;按钮单击的效果取决于列表框中的选择。这样做的好处是:在列表框中,您可以存储任何数据,而不仅仅是字符串;列表框将显示元素类型的 GetString 返回的内容。



我提到了代码示例中的闭包。理解这一点非常重要:

http://en.wikipedia.org / wiki / Closure_%28computer_science%29 [ ^ ]。



-SA


是。

当您输入事件处理程序时,您将获得两个参数: sender 这是对象 e 派生自 EventArgs

发件人参数是导致事件发生的控件。因此,如果您有20个按钮,并将所有Click事件路由到同一个处理程序,则 sender 将是单击的按钮。只需将其转换为按钮,您就可以使用它来处理事件:

 私有  Sub  MyButton_Click(发件人作为 对象,e  As  EventArgs)句柄 Button1.Click,Button2.Click,... 
Dim b As Button = TryCast (发件人,按钮)
如果 b IsNot Nothing 然后
Console.WriteLine(b.Text)
结束 如果
结束 Sub


is there any way to handle all click event in 1 sub? i have 20 buttons.

解决方案

Yes. It should be something like:

foreach(Button button in allButtons) {
    button.Click += (sender, eventArgs) {
        ButtonClickHandler((Button)sender); // pay attention for type cast:
                                            // sender is guaranteed to have a runtime type Button
    };
}

void ButtonClickHandler(Button button) {
    // do something
}



But now, the problem is: OK, you use one handler for several buttons in this or some other ways. But do you really need to do exactly the same thing in response to a click of any of those buttons? Hardly. I would rather assume you want to do something different.

Here is what you would be tempted to do, but should not:

void ButtonClickHandler(Button button) {
    if (button == myFirstButton) // ugly!
        DoOneThing(); // ugly!
    else if (button == mySeconfButton) // ugly!
        DoSomethineElse(); // ugly!
    // ... even uglier!
}



Can you see that it would completely defeat the purpose of one unified handler method? It could be much worse than adding its individual handler to each button. One common handler can only be useful when the handling it totally identical for all button, only data is different. But how to pass this data? In most cases, it would be enough to pass just the index of the button in the array, or something like that. It depends on the purpose of the buttons.

Something like this:

for(int index = 0; index < allButtons.Length; ++index) {
    allButtons[index].Click += (sender, eventArgs) {
        ButtonClickHandler(index); // here, is a non-trivial feature is used: the closure
    };
}

void ButtonClickHandler(int index) {
    // do something using index as a parameter to some data array/collection
}



Are you getting the idea?

To finish with the critical review of the idea of using the common handler, I need to note: the whole idea of using 20 buttons is questionable, and this is a root of the problem. Why? You could think about a list box and one button; the effect of button click would depends on the selection in the list box. The benefit of this would be this: in a list box, you can store any data, not just the string; the list box will display whatever is returned by GetString of the element type.

I mentioned the closure in the code sample. It''s very important to understand:
http://en.wikipedia.org/wiki/Closure_%28computer_science%29[^].

—SA


Yes.
When you enter an event handler, you are given two parameters: sender which is an object, and e which is derived from EventArgs.
The sender parameter is the control which caused the event to occur. So if you have 20 buttons, and route all the Click events to the same handler, the sender will be the button that was clicked. Just cast it to a button, and you can use it to process the event:

Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, ...
    Dim b As Button = TryCast(sender, Button)
    If b IsNot Nothing Then
        Console.WriteLine(b.Text)
    End If
End Sub


这篇关于关于Click事件[VB .NET]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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