bjwade62 Posted October 5, 2006 Posted October 5, 2006 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. Quote
techmanbd Posted October 5, 2006 Posted October 5, 2006 Private Sub mnuHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHello.Click strMenuText = mnuHello.text End Sub Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
Cags Posted October 5, 2006 Posted October 5, 2006 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 Quote Anybody looking for a graduate programmer (Midlands, England)?
bjwade62 Posted October 5, 2006 Author Posted October 5, 2006 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 Quote
bjwade62 Posted October 6, 2006 Author Posted October 6, 2006 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 Quote
Leaders snarfblam Posted October 6, 2006 Leaders Posted October 6, 2006 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 Quote [sIGPIC]e[/sIGPIC]
bjwade62 Posted October 6, 2006 Author Posted October 6, 2006 Oh, ok, I see now. Thanks Marble Eater. One question... is there a limit? I could easily have over 100 items. Thanks again, Bernie Quote
Leaders snarfblam Posted October 7, 2006 Leaders Posted October 7, 2006 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.