VB.NET: Can I rewire button events to handle several different button clicks through one click event

I have a form with 5 buttons that simply click through a datatable.  The buttons are FIRST, LAST, PREVIOUS, NEXT and NEW.  For the sake of discussion lets say the buttons are named btnFIRST, btnLAST, etc...
Instead of coding through 5 separate button events to call one subroutine (and pass the button object to the subroutine), is there a way that I can do an AddHandler or something to allow the one added event to handle  the clicks of any and all buttons?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Sure...you can either:

    Use the "Handles" keyword and list MULTIPLE controls afterwards
    < or > 
    Wire them all up at run-time with AddHandler()

In either case, the "sender" parameter will tell you which button fired the event:
    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFIRST.Click, btnLAST.Click, btnPREVIOUS. Click, btnNEXT.Click, btnNEW.Click
        Dim SourceButton As Button = CType(sender, Button)
        ' ... do something with "SourceButton" ...
    End Sub

Open in new window

John (Yiannis) ToutountzoglouConnect With a Mentor Instructor Multiengine PilotCommented:
select all your button ...Go to properties -Events and double click the click event...this add a button click event handling all the buttons...
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Ok i quit!!!!!@Idle's here!!!!! :):):)
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...you gotta be fast around here!  ;)

I'm gonna go mow my lawn so I won't be around for awhile...have fun jtoutou!   =D
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
i do not know how fun is for you to mow the grass but i love it ....!!!
LiebertUserAuthor Commented:
First and foremost, both of your solutions are terrific because I had no cliue they existed.  I was aware of Addhandler(), and planned on wiring the events at run time... but if I did that, then anytime I clicked on one of the buttons in the GUI designer it would create a new solo event for me.

I didn't know about the multiple handles capability of HANDLES...  I added
Handles btnFIRST.Click, btnLAST.Click, btnPREVIOUS. Click, btnNEXT.Click, btnNEW.Click

Now when I click on any of the buttons the GU designer redirects me to the one event!!! It's perfect!  Thanks!!!
I'm splitting the points and getting out of here before a fight breaks out....  I got what I needed <grin>!

LiebertUserAuthor Commented:
I meant to close the question in my response ID 33727285.  I got in a hurry and clicked the wrong thing.  Anyway, close it, award the points, they answered it perfectly.
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.

All Courses

From novice to tech pro — start learning today.