Solved

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

Posted on 2014-11-22
6
828 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 86

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 DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

738 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