Vb.net Textbox Command Button

Windows form
============
I hve a windows form with 2 TextBoxes and 2 Cancel buttons. As a textboxes value can be cancelled .

I populate the data from the dataset (well i dont bind it as its not ncesssary)
in both the textboxes.  User can click cancel to cancel a value in textbox  where I update the database saying that line has been cancelled.
How would we know that if cancel button pressed corresponds to textbox1 or textbox2
 

Textbox1              cancelButton
Textbox2              cancelButton
Textbox3              cancelButton

Can a simple example be shown ? thanks in advance
sainavya1215Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arif_eqbalCommented:
You surely will have different names for the three Cancel Buttons
Say ButtonCancel_1, ButtonCancel_2 and so on

Solution 1:

Handle the Click events of the Cancel Buttons separately

so ButtonCancel_1 Click means textBox1 value was cancelled
 ButtonCancel_2 Click means textBox2 value was cancelled

Solution 2:

use a common Event handler for Click of all such cancel Buttons
Then use

CType(sender, Button).Name to identify the name of the Cancel Button that was clicked


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sainavya1215Author Commented:
yep....Undestood solution 1 ..........could it be more explained on solution 2.... Thanks for that
0
ptakjaCommented:
When an event handler is fired, it is passed an Object called "sender". This is the object that triggered the event.

If you know what type of object it is, you can use the CTYPE function to cast the generic Object into the object that triggered the event.

Solution 2 provided by arif_egbal simply says this:

Private Sub ButtonClicks(sender As Object, e as EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    Select Case CType(sender, Button).Name     ' This determines WHAT button the user clicked.
        Case "Button1"

        Case "Button2"

        Case "Button3"


End Sub

This technique allows you to reduce code by using the same event handler to handle multiple events from objects on your form.

Hope that helps.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.