Solved

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

Posted on 2014-11-22
6
728 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 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