Solved

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

Posted on 2010-09-21
8
449 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - Project Software to help the Developer 7 33
How to join on ID, with prefix? 15 58
Help with exporting to excel 4 26
VB.Net Tranfer data between to VB.Net apps 8 25
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

786 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