VB.NET 对动态添加的 ToolStripMenuItem 的事件执行代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15502532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:51:49  来源:igfitidea点击:

VB.NET Execute code on events for dynamically added ToolStripMenuItem

vb.netdynamictoolstripmenutoolstripitem

提问by Shraavan Acharya

VB.NET
On the opening of a menu item (i.e. the top-level menu item), i have added ToolStripMenuItem (i.e. DropDownItem) to the menu item at runtime.

The ToolStripMenuItems added by me during runtime are the names of the forms active in the current project.

Whenever the ToolStripMenuItem with a particular form name is clicked, the form should be given focus.

How can i execute the desired code for the event of a dynamically added ToolStripMenuItem?

VB.NET
在打开菜单项(即顶级菜单项)时,我在运行时将ToolStripMenuItem(即DropDownItem)添加到菜单项中。

我在运行时添加的 ToolStripMenuItems 是当前项目中活动的表单的名称。

每当单击具有特定表单名称的 ToolStripMenuItem 时,都应为该表单提供焦点。

如何为动态添加的 ToolStripMenuItem 的事件执行所需的代码?

Private Sub WindowToolStripMenuItem_DropDownOpening(sender As Object, e As System.EventArgs) Handles WindowToolStripMenuItem.DropDownOpening
        WindowToolStripMenuItem.DropDown.Items.Clear()

        For Each Form In My.Application.OpenForms
            If Not Form.name = frmLogin.Name And Not Form.name = Me.Name Then
                Dim tmiForm = New ToolStripMenuItem()
                tmiForm.Name = Form.name
                tmiForm.Text = Form.text
                WindowToolStripMenuItem.DropDownItems.Add(tmiForm)
            End If
        Next

    End Sub


i want to give focus to a form based on the tmiForm's click event...
i tried searching on the web i only got results for C#


我想将焦点放在基于 tmiForm 的点击事件的表单上...
我尝试在网上搜索我只得到 C# 的结果

回答by Neolisk

Use AddHandler:

使用AddHandler

AddHandler tmiForm.Click, AddressOf ClickHandler

Here is how you can write your ClickHandler:

以下是您如何编写您的ClickHandler

Public Sub ClickHandler(ByVal sender As Object, ByVal e As EventArgs)
  'for a condition based on a ToolStripMenuItem that fired it
  'If CType(sender, ToolStripMenuItem).Name ...
End Sub

回答by Shraavan Acharya

try this-

尝试这个-

Private Sub clickeventhandler(sender As Object, e As EventArgs)
    For Each Form In My.Application.OpenForms
        If CType(sender, ToolStripMenuItem).Name = Form.Name Then
            Form.Focus()
            Exit Sub
        End If
    Next
End Sub

your previous code seems fine just add a single line.
After

您之前的代码看起来不错,只需添加一行即可。

WindowToolStripMenuItem.DropDownItems.Add(tmiForm)

write this-

写这个——

AddHandler tmiForm.Click, AddressOf clickeventhandler

回答by Rod

I used a simpler approach. When you click on the menustrip icon, a small arrow appears in the upper-right-hand portion of the window. Click the arrow to open a menu properties window. You can set the visible properties to control what will be seen on the initial menu. You can also set or clear the visible attribute in code:

我使用了一种更简单的方法。当您单击菜单条图标时,窗口的右上角会出现一个小箭头。单击箭头打开菜单属性窗口。您可以设置可见属性来控制将在初始菜单上看到的内容。您还可以在代码中设置或清除可见属性:

    Public Sub MenuManage(Wayside As Integer, Vehicle As Integer, _
    System As Integer, Tools As Integer, Reports As Integer, _
    Edit As Integer, Zoom As Integer)

    Main.WaysideToolStripMenuItem.Visible = Wayside
    Main.VehicleToolStripMenuItem.Visible = Vehicle
    Main.SystemToolStripMenuItem.Visible = System
    Main.ToolsToolStripMenuItem1.Visible = Tools
    Main.ReportsToolStripMenuItem.Visible = Reports
    Main.EditToolStripMenuItem.Visible = Edit
    Main.ZoomToolStripMenuItem.Visible = Zoom

    End Sub

Within the Load and FormClosed event code, control what is seen on the menu:

在 Load 和 FormClosed 事件代码中,控制菜单上显示的内容:

Call MenuManage(True, True, True, True, True, False, False)

调用 MenuManage(True, True, True, True, True, False, False)