Domanda

What I Wish to achieve is to extract the select item text and display it into a messagebox (for start, then i'm going to use it for a SQL Query...)

I want to extract that particular selected ITEM, for instance : "SPR (Suivi piece rechange)" in the following image :

enter image description here

I tried this, but when I click on "Menu", it returns the name of my menu strip "MenuStrip1" :

Private Sub MenuStrip1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuStrip1.Click
    MessageBox.Show(DirectCast(sender, MenuStrip).Name)
End Sub

EDIT:

I forgot to mention that all the ITEMS are added dynamically from the database, so there will be no predefined Private Sub....End Sub Procedure for these Items.

Thanks in advance.

È stato utile?

Soluzione

The MenuStrip object only refers to the actual menu strip itself, not the individual menu items, which are actually ToolStripMenuItem objects. You're looking for the Text property of those objects. For example:

DirectCast(YourDynamicMenuItemObjectHere, ToolStripMenuItem).Text

If you're looking for a way to capture events, you'll need to create a generic event handler:

Private Sub GenericMenuItem_Click(sender As System.Object, e As System.EventArgs)
    MessageBox.Show(DirectCast(sender, ToolStripMenuItem).Text)
    'Whatever else you need to do based on the text of the menu item
End Sub

And then hook that handler to the menu items whenever they're created:

'Code that creates YourDynamicallyGeneratedMenuItem
AddHandler YourDynamicallyGeneratedMenuItem.Click, AddressOf GenericMenuItem_Click
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top