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
我想重点关注基于tmiForm点击事件的表单......
我试着在网上搜索我只得到C#的结果
答案 0 :(得分:3)
使用AddHandler:
AddHandler tmiForm.Click, AddressOf 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
答案 1 :(得分:0)
试试这个 -
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
您之前的代码似乎很好,只需添加一行即可。
后
WindowToolStripMenuItem.DropDownItems.Add(tmiForm)
写下这个 -
AddHandler tmiForm.Click, AddressOf clickeventhandler
答案 2 :(得分:0)
我使用了一种更简单的方法。单击menustrip图标时,窗口的右上角会出现一个小箭头。单击箭头以打开菜单属性窗口。您可以设置可见属性以控制在初始菜单上看到的内容。您还可以在代码中设置或清除visible属性:
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
在Load和FormClosed事件代码中,控制菜单上显示的内容:
调用MenuManage(真,真,真,真,真,假,假)