Click here to Skip to main content
13,003,102 members (57,411 online)
Rate this:
Please Sign up or sign in to vote.
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 19-Nov-12 3:40am
VitorHugoGarcia 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
VitorHugoGarcia 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.
VitorHugoGarcia 19-Nov-12 12:34pm
n.podbielski 19-Nov-12 13:09pm
Geez... Is this so hard to understand?
Sergey Alexandrovich Kryukov 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.
n.podbielski 19-Nov-12 14:29pm
That was to Hugo SA. :)
Sergey Alexandrovich Kryukov 19-Nov-12 15:29pm
Anyway, I answered, because the idea is the same for all cases, come to think about.
n.podbielski 19-Nov-12 15:58pm
And for 10 rep points... right? :)
Sergey Alexandrovich Kryukov 19-Nov-12 16:04pm
I don't know what points are you talking about. For the answer? Why?
n.podbielski 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 19-Nov-12 16:34pm
do an if test for the empty text boxes and skip your validation routine

1 solution

Rate this: bad
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,[^]). 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
        '... 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.

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170626.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
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