【问题标题】:How to display Drop Down item TEXT from Menu Strip如何从菜单条显示下拉项文本
【发布时间】:2013-07-18 19:53:05
【问题描述】:

我希望实现的是提取选择项文本并将其显示到消息框中(开始,然后我将其用于 SQL 查询...)

我想提取特定的选定项目,例如:下图中的“SPR(Suivipiece rechange)”:

我试过这个,但是当我点击“菜单”时,它会返回我的菜单条“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

编辑:

我忘了提到所有的项目都是从数据库中动态添加的, 所以不会有预定义的 Private Sub....这些 Item 的 End Sub 过程。

提前致谢。

【问题讨论】:

    标签: vb.net menustrip


    【解决方案1】:

    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
    

    【讨论】:

    • 谢谢,最后我用嵌套的 for 循环(3 级)完成了
    猜你喜欢
    • 1970-01-01
    • 2018-08-16
    • 2021-01-02
    • 1970-01-01
    • 2014-09-28
    • 2020-05-14
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多