Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-11-22
6
Medium Priority
?
944 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 45

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 45

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses

963 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