Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted
Private Sub mnuHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHello.Click
  strMenuText = mnuHello.text
End Sub

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

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.

' 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

Anybody looking for a graduate programmer (Midlands, England)?
Posted

Ecellent! Thanks Cags!!

 

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.

' 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

Posted

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

 

   Private Sub RelocatableModularBuildingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RelocatableModularBuildingsToolStripMenuItem.Click
       WebSearch = sender.ToString
   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.

' 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

  • Leaders
Posted

You can specify multiple controls or events in the Handles clause, like so:

   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

[sIGPIC]e[/sIGPIC]
  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...