Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 19-Nov-12 3:40am
Comments
VitorHugoGarcia at 19-Nov-12 9:43am
   
You could check which control has focus and if its not = "" inside eventhandler and act accordingly
DileepkumarReddy at 19-Nov-12 9:51am
   
Thanks for the suggestion..its working
VitorHugoGarcia at 19-Nov-12 10:01am
   
I'm glad :)
Sergey Alexandrovich Kryukov at 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
VitorHugoGarcia at 19-Nov-12 12:34pm
   
?
n.podbielski at 19-Nov-12 13:09pm
   
Geez... Is this so hard to understand?
Sergey Alexandrovich Kryukov at 19-Nov-12 14:16pm
   
Yes. I can least some 4 different types under that name. It's just a matter of waste of time: should anyone explain the trivial solution in all well-known cases? UI library should be tagged.
--SA
n.podbielski at 19-Nov-12 14:29pm
   
That was to Hugo SA. :)
Sergey Alexandrovich Kryukov at 19-Nov-12 15:29pm
   
Anyway, I answered, because the idea is the same for all cases, come to think about.
--SA
n.podbielski at 19-Nov-12 15:58pm
   
And for 10 rep points... right? :)
Sergey Alexandrovich Kryukov at 19-Nov-12 16:04pm
   
I don't know what points are you talking about. For the answer? Why?
--SA
n.podbielski at 19-Nov-12 17:31pm
   
Don't think about this much. It's Monday's evening and I probably have to much to drink :)
And yes I was talking about CP repution pts.
Mathlab at 19-Nov-12 16:34pm
   
do an if test for the empty text boxes and skip your validation routine

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
 
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
 
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
  Permalink  
v6
Comments
Espen Harlinn at 19-Nov-12 19:15pm
   
Well done :-D
Sergey Alexandrovich Kryukov at 19-Nov-12 19:29pm
   
Thank you, Espen.
--SA
VitorHugoGarcia at 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 at 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)

  Print Answers RSS
0 CPallini 365
1 Sergey Alexandrovich Kryukov 242
2 OriginalGriff 165
3 George Jonsson 159
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,860
2 CPallini 5,135
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100