Solved

How to determine which toolstrip item/button was pressed in VB

Posted on 2014-11-22
6
696 Views
Last Modified: 2014-11-22
There is a very simple answer to this, but I can't remember...

In the Toolstrip1_ItemClicked event, what code is used to determine which item/button was pressed?

My code will execute like this in the ItemClicked event to handle all toolstrip clicks (is this the best place to put it?):

Dim item as ? = ToolStrip1.?
Select Case item
    Case "New"
        AddRecord
End Select

Thanks!
0
Comment
Question by:Sheryl Landis
  • 3
  • 2
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40459515
Why not have one event handler per Item/button?  That is the usual way in my experience.
0
 

Author Comment

by:Sheryl Landis
ID: 40459521
Because I am running routines depending on which TabControl tab is selected.  The ToolStrip handles all the form's data entry/reporting operations.  I would have to repeat code in each event handler otherwise.
0
 

Author Comment

by:Sheryl Landis
ID: 40459547
I think this is what I'm looking for:

Dim item As String = e.ClickedItem.ToString

Any other ideas?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40459554
You don't have to repeat code.  Just make one routine that handles all the buttons together:
    Private Sub ToolStripButtons_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click, ToolStripButton2.Click, ToolStripButton3.Click
        Dim btn As ToolStripButton = CType(sender, ToolStripButton)
        Select Case btn.Name
            Case "ToolStripButton1"
                Debug.Print("1")
            Case "ToolStripButton2"
                Debug.Print("2")
            Case "ToolStripButton3"
                Debug.Print("3")
        End Select
    End Sub

Open in new window

*Obviously putting in more code to handle which Tab is selected...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40459576
>>Because I am running routines depending on which TabControl tab is selected.
But then you need code to determine which button was pressed and which bit of code needs to be run.

>> I would have to repeat code in each event handler otherwise.
No.  Have a common subroutine that is called from the appropriate handlers.

Anyway.  Dim item As String = e.ClickedItem.ToString
That should be OK to determine the button/item being clicked.
0
 

Author Closing Comment

by:Sheryl Landis
ID: 40459793
Your code is very helpful:  Dim btn As ToolStripButton = CType(sender, ToolStripButton)

This is the code I ended up using:

 Private Sub ToolStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
        RunThis(e.ClickedItem.ToString)  
 End Sub

Private Sub RunThis(menuItem As String)
        Dim s As String = tabControl.TabPages(tabControl.SelectedIndex).Name
        Select Case s
             Case "Names"
                 Select Case menuItem
                     Case "Add"
                         bIsNew = True
                         etc.........                        
               
Thank you for your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

23 Experts available now in Live!

Get 1:1 Help Now