Solved

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

Posted on 2010-09-21
8
443 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 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 249 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
 
LVL 85

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
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 keystrokes in Netbeans IDE 8.0 for Windows.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now