Avatar of Sheryl Landis
Sheryl Landis
 asked on

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

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!
Visual Basic Classic.NET Programming

Avatar of undefined
Last Comment
Sheryl Landis

8/22/2022 - Mon
AndyAinscow

Why not have one event handler per Item/button?  That is the usual way in my experience.
Sheryl Landis

ASKER
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.
Sheryl Landis

ASKER
I think this is what I'm looking for:

Dim item As String = e.ClickedItem.ToString

Any other ideas?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Mike Tomlinson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
AndyAinscow

>>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.
Sheryl Landis

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