[英]How to display Drop Down item TEXT from Menu Strip
我希望實現的是提取選擇項文本並將其顯示在消息框中(開始時,然后我將其用於SQL查詢...)
我要提取特定選擇的ITEM,例如:下圖中的“ SPR(Suivi件更換)”:
我嘗試了這個,但是當我單擊“菜單”時,它返回菜單條“ 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
編輯:
我忘了提到所有ITEMS是從數據庫中動態添加的,因此這些項目將沒有預定義的私有子程序...結束子程序。
提前致謝。
MenuStrip對象僅引用實際的菜單欄本身,而不引用單個菜單項,而這些菜單項實際上是ToolStripMenuItem對象。 您正在尋找這些對象的Text屬性。 例如:
DirectCast(YourDynamicMenuItemObjectHere, ToolStripMenuItem).Text
如果您正在尋找一種捕獲事件的方法,則需要創建一個通用事件處理程序:
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
然后在創建菜單項時將該處理程序掛接到菜單項:
'Code that creates YourDynamicallyGeneratedMenuItem
AddHandler YourDynamicallyGeneratedMenuItem.Click, AddressOf GenericMenuItem_Click
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.