?
Solved

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

Posted on 2010-09-21
8
Medium Priority
?
452 Views
Last Modified: 2013-11-26
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?
0
Comment
Question by:LiebertUser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 33727159
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

0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 996 total points
ID: 33727174
select all your button ...Go to properties -Events and double click the click event...this add a button click event handling all the buttons...
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33727206
Ok i quit!!!!!@Idle's here!!!!! :):):)
0
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33727227
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
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33727242
u2!!
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33727276
i do not know how fun is for you to mow the grass but i love it ....!!!
0
 

Author Comment

by:LiebertUser
ID: 33727285
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>!

0
 

Author Comment

by:LiebertUser
ID: 33727318
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.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question