如何检查单击了哪个按钮 [英] How to check which button is clicked

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

问题描述

我有一个文本框,在经过验证的事件中,我正在获取一些详细信息.
我有一个清除所有内容的清除按钮.问题是当用户在文本框中键入内容并按清除文本框的已验证事件正在引发时.我想防止这种情况..

帮帮我.谢谢advacne

I have an textbox,in that validated event i am retreiving some details.
I have clear button which clears all.Issue is when users types something in textbox and presses clear textbox''s validated event is raising..i want to prevent that..

Help me out..thanks in advacne

推荐答案

可以知道单击了什么按钮,但是在许多情况下,这是错误的方法.在大多数情况下,只需使用单独的按钮处理每个按钮.问题很简单:真正重用然后编写代码的地方,以及只是重复自己的地方(最重要的规则之一:不要重复自己,
It is possible to know what button is clicked, but in many cases, this is wrong approach. In most cases, you just handle each button with its separate handle. The problem is simple: where you really reuse then code, and where you just Repeat Yourself (one of the most important rules: don''t repeat yourself, http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^]). Well, let''s compare:

Dim buttonFirst As System.Windows.Forms.Button = '...
Dim buttonSecond As System.Windows.Forms.Button = '...

'...

AddHandler buttonFirst.Click, Address of HandleButtonClick 
AddHandler buttonSecond.Click, Address of HandleButtonClick 

'...

Private Sub HandleButtonClick(sender As Object, eventArgs As System.EventArgs)
    Dim buffon As System.Windows.Forms.Button = DirectCast(sender, System.Windows.Forms.Button)
    If button = buttonFirst Then ' ugly!
        '... do something on fist button click
    Else
        '... do something on second button click
    End If
End Sub



在上面的示例中,您在两个处理程序之间重用了一种方法,但是您是否重用了代码?不,您只是在"if"语句中添加了丑陋的内容.

那么,各个处理程序呢?让我们看看:



In the sample above, you reused one method between two handlers, but did you reused the code? No, you just pushed ugliness into "if" statement.

So, how about individual handlers then? Let''s see:

AddHandler buttonFirst.Click, Sub(sender As Object, eventArgs As System.EventArgs) '... important -- anonymous handler creates loos coupling with code
    '... because you can call any method(s) here, not necessary with these two parameters...
    '... do something on fist button click
End Sub

AddHandler buttonSecond.Click, Sub(sender As Object, eventArgs As System.EventArgs)
    '... do something on second button click
End Sub



更好吗?这取决于您要编写的内容,而不是用"..."描述的代码.

结论.您可以对不同控件的几个事件使用相同的方法,但是只有在这些处理程序应该执行确实非常相似的操作时,它才有意义,此外,它需要类型区分大小写并实际上使用第一个参数.如果只是两个按钮(或其他实例)执行不同的操作,则最好使用单独的方法来处理每个事件.

—SA



Isn''t it better? It depends on what you want to write instead of the code depicted with "...".

Conclusion. You can use the same method for several several events of different controls, but it pays off only if these handlers should do something really similar, besides, it requires type case and actually using first parameter. If it''s just two button (or something else) instances doing something different, it''s much better to use individual methods for handling the event on each.

—SA


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

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