Getting the text of a selected menustrip item

bjwade62

Centurion
Joined
Oct 31, 2003
Messages
104
Does anyone know how to get the text of a selected menustrip item?


Here's a poor representation of a menu strip item, but you get the idea.

File
|
Open

I'm looking for a way to get the text "Open" from the menuitem.
 
Visual Basic:
Private Sub mnuHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHello.Click
   strMenuText = mnuHello.text
End Sub
 
techmanbds' suggestion would work, but would require coding every single menu item individually at design time, which isn't possible if it's a dynamically created item. A more generic method could be used by attaching an event handler that simply casts the sender object then gets the text.
Visual Basic:
' your method would look something like this
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim mnuItem As MenuItem = DirectCast(sender, MenuItem)
    MessageBox.Show(mnuItem.Text)
End Sub
' 
' and just so you know, it would be attached to the event like so
AddHandler MenuItem2.Click, AddressOf MenuItem2_Click
 
Ecellent! Thanks Cags!!

Cags said:
techmanbds' suggestion would work, but would require coding every single menu item individually at design time, which isn't possible if it's a dynamically created item. A more generic method could be used by attaching an event handler that simply casts the sender object then gets the text.
Visual Basic:
' your method would look something like this
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim mnuItem As MenuItem = DirectCast(sender, MenuItem)
    MessageBox.Show(mnuItem.Text)
End Sub
' 
' and just so you know, it would be attached to the event like so
AddHandler MenuItem2.Click, AddressOf MenuItem2_Click
 
Greeting Cags,
I'm pretty sure your code is what I'm looking for. However, I'm not able to use it properly. Maybe I'm not describing what I need well enough.

I have a menustrip with each item's text set. I want to get the text of each item as they are clicked. I then pass it into a variable called WebSearch. Below is what I'm using now. However it does require me to code it into each menuitem. Sounds like your way will be considerable less code if I can use it properly. Can you give me more detail?

Thanks, I really appreciate it!
Bernie

Visual Basic:
    Private Sub RelocatableModularBuildingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RelocatableModularBuildingsToolStripMenuItem.Click
        WebSearch = sender.ToString
    End Sub

Cags said:
techmanbds' suggestion would work, but would require coding every single menu item individually at design time, which isn't possible if it's a dynamically created item. A more generic method could be used by attaching an event handler that simply casts the sender object then gets the text.
Visual Basic:
' your method would look something like this
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim mnuItem As MenuItem = DirectCast(sender, MenuItem)
    MessageBox.Show(mnuItem.Text)
End Sub
' 
' and just so you know, it would be attached to the event like so
AddHandler MenuItem2.Click, AddressOf MenuItem2_Click
 
You can specify multiple controls or events in the Handles clause, like so:
Visual Basic:
    Private Sub AnyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
[I]        Handles RelocatableModularBuildingsToolStripMenuItem.Click,
            SomeOtherToolStripItem.Click,
            YetAnotherToolStripItem.Click,
            OneMoreToolStripItem.Click[/I]
 
        WebSearch = DirectCast(sender, ToolStripItem).Text
    End Sub
 
Oh, ok, I see now. Thanks Marble Eater. One question... is there a limit? I could easily have over 100 items.

Thanks again,
Bernie
 
No limit, but wouldn't that be awefully time consuming to design in the IDE? At this point you might simply want to store the data for the buttons in an XML or TXT file and at runtime read the data and create the buttons and attach handlers dynamically. (This would also allow you to expand the options later without re-compiling the EXE.)

If you prefer, though, there is nothing stopping you from creating as many tool strip buttons at design time as you like and using the same handler.
 
Back
Top