Private Function GetMenuStripSourceControl(vMenuItem As ToolStripMenuItem) As Control Dim ans As Control = Nothing Try Dim obj As Object = vMenuItem.GetCurrentParent() Select Case obj.GetType Case GetType(ContextMenuStrip) ans = CType(obj, ContextMenuStrip).SourceControl Case GetType(ToolStripDropDownMenu) ans = GetMenuStripSourceControl(CType(obj, ToolStripDropDownMenu).OwnerItem) End Select Catch ex As Exception Throw (ex) End Try Return ans End Function
Private Sub SomeLayerToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SomeLayerToolStripMenuItem.Click Dim tmpMenuItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim sourceCtl As Control = GetMenuStripSourceControl(tmpMenuItem)
匿名 2023/04/1 (Sat) 13:36:17
Private Function GetMenuStripSourceControl(vMenuItem As ToolStripMenuItem) As Control
Dim ans As Control = Nothing
Try
Dim obj As Object = vMenuItem.GetCurrentParent()
Select Case obj.GetType
Case GetType(ContextMenuStrip)
ans = CType(obj, ContextMenuStrip).SourceControl
Case GetType(ToolStripDropDownMenu)
ans = GetMenuStripSourceControl(CType(obj, ToolStripDropDownMenu).OwnerItem)
End Select
Catch ex As Exception
Throw (ex)
End Try
Return ans
End Function
Private Sub SomeLayerToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SomeLayerToolStripMenuItem.Click
Dim tmpMenuItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim sourceCtl As Control = GetMenuStripSourceControl(tmpMenuItem)
End Sub