Solved

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

Posted on 2014-11-22
6
750 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
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

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How useful is the free version of Selenium? 3 32
Adding Existing Item to vb.net Project causes Errors 4 31
Batch file or script with password 22 42
Store results in vb.net 3 20
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

776 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