Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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
Posted
Comments
_Vitor Garcia_ 19-Nov-12 9:43am    
You could check which control has focus and if its not = "" inside eventhandler and act accordingly
DileepkumarReddy 19-Nov-12 9:51am    
Thanks for the suggestion..its working
_Vitor Garcia_ 19-Nov-12 10:01am    
I'm glad :)
Sergey Alexandrovich Kryukov 19-Nov-12 12:02pm    
What is button? If the class is "Button", it can be several different classes. So, full name, please; and always tag UI library you use.
--SA
_Vitor Garcia_ 19-Nov-12 12:34pm    
?

1 solution

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:

VB
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


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:

VB
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


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
 
Share this answer
 
v6
Comments
Espen Harlinn 19-Nov-12 19:15pm    
Well done :-D
Sergey Alexandrovich Kryukov 19-Nov-12 19:29pm    
Thank you, Espen.
--SA
_Vitor Garcia_ 20-Nov-12 4:35am    
Nice SA. You nailed it, as usual :)
As for the button doubt, it was clear to me and i still dont get what is all the fuzz about. The truth is that given the question I tried to help Dillep and apparently I did.
Sergey Alexandrovich Kryukov 20-Nov-12 11:23am    
Thank you, Vitor Hugo.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900