Author:水如烟
'测试一
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'列出所有菜单项名称和标题
Dim t As Hashtable = LzmTW.Common.MenuItemClass.GetMenuItemsNameAndText(Me, Me.MainMenu1)
For Each o As System.Collections.DictionaryEntry In t
Console.WriteLine("MenuItem Name: {0}, Text: {1}", o.Key, o.Value)
Next
'列出所给菜单名称
Console.WriteLine(LzmTW.Common.MenuItemClass.GetMenuItemName(Me, Me.OpenMenuItem))
'根据菜单名称修改标题
LzmTW.Common.MenuItemClass.GetMenuItemByName(Me, "OpenMenuItem").Text = "Hello (&H)"
'列出所给菜单标题
Console.WriteLine(LzmTW.Common.MenuItemClass.GetMenuItemByName(Me, "OpenMenuItem").Text)
End Sub
'输出结果:
'MenuItem Name: HelpMenuItem, Text: Help
'MenuItem Name: ExitMenuItem, Text: Exit
'MenuItem Name: ViewMenuItem, Text: View
'MenuItem Name: OpenMenuItem, Text: Open
'MenuItem Name: AboutMenuItem, Text: About
'MenuItem Name: FileMenuItem, Text: File
'OpenMenuItem
'Hello (&H)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'列出所有菜单项名称和标题
Dim t As Hashtable = LzmTW.Common.MenuItemClass.GetMenuItemsNameAndText(Me, Me.MainMenu1)
For Each o As System.Collections.DictionaryEntry In t
Console.WriteLine("MenuItem Name: {0}, Text: {1}", o.Key, o.Value)
Next
'列出所给菜单名称
Console.WriteLine(LzmTW.Common.MenuItemClass.GetMenuItemName(Me, Me.OpenMenuItem))
'根据菜单名称修改标题
LzmTW.Common.MenuItemClass.GetMenuItemByName(Me, "OpenMenuItem").Text = "Hello (&H)"
'列出所给菜单标题
Console.WriteLine(LzmTW.Common.MenuItemClass.GetMenuItemByName(Me, "OpenMenuItem").Text)
End Sub
'输出结果:
'MenuItem Name: HelpMenuItem, Text: Help
'MenuItem Name: ExitMenuItem, Text: Exit
'MenuItem Name: ViewMenuItem, Text: View
'MenuItem Name: OpenMenuItem, Text: Open
'MenuItem Name: AboutMenuItem, Text: About
'MenuItem Name: FileMenuItem, Text: File
'OpenMenuItem
'Hello (&H)
'测试二
Private Sub FileMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileMenuItem.Select, AboutMenuItem.Click, ExitMenuItem.Click, HelpMenuItem.Click, OpenMenuItem.Click, ViewMenuItem.Select
Select Case LzmTW.Common.MenuItemClass.GetMenuItemName(Me, CType(sender, MenuItem))
Case "ExitMenuItem"
Me.Text = "Exit"
Case "ViewMenuItem"
Me.Text = "View"
Case "OpenMenuItem"
Me.Text = "Open"
Case "AboutMenuItem"
Me.Text = "About"
Case "FileMenuItem"
Me.Text = "File"
Case "HelpMenuItem"
Me.Text = "Help"
End Select
End Sub
Private Sub FileMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileMenuItem.Select, AboutMenuItem.Click, ExitMenuItem.Click, HelpMenuItem.Click, OpenMenuItem.Click, ViewMenuItem.Select
Select Case LzmTW.Common.MenuItemClass.GetMenuItemName(Me, CType(sender, MenuItem))
Case "ExitMenuItem"
Me.Text = "Exit"
Case "ViewMenuItem"
Me.Text = "View"
Case "OpenMenuItem"
Me.Text = "Open"
Case "AboutMenuItem"
Me.Text = "About"
Case "FileMenuItem"
Me.Text = "File"
Case "HelpMenuItem"
Me.Text = "Help"
End Select
End Sub
Imports System.Windows.Forms
Imports System.Reflection
Namespace LzmTW.Common
''' -----------------------------------------------------------------------------
''' Project : CommonTest
''' Class : LzmTW.Common.MenuItemClass
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' 菜单操作
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [lzmtw] 2005-10-21 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class MenuItemClass
Private Const FindBinding As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public
Private Shared tmpHashtable As New Hashtable
Private Shared tmpForm As Form
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回主菜单所有菜单项的名称和标题
''' </summary>
''' <param name="FormIstance">窗体实例</param>
''' <param name="MainMenu">主菜单</param>
''' <returns>名称和标题列表</returns>
''' <remarks>
''' 对应于哈希表的键和值
''' </remarks>
''' <history>
''' [lzmtw] 2005-10-21 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function GetMenuItemsNameAndText(ByVal FormIstance As Form, ByVal MainMenu As MainMenu) As Hashtable
tmpForm = FormIstance
tmpHashtable.Clear()
For Each o As MenuItem In MainMenu.MenuItems
GetMenuItemsNameAndText(o)
Next
Return tmpHashtable
End Function
Private Shared Sub GetMenuItemsNameAndText(ByVal oMenuItem As MenuItem)
tmpHashtable.Add(GetMenuItemName(tmpForm, oMenuItem), oMenuItem.Text)
If oMenuItem.IsParent Then
For Each m As MenuItem In oMenuItem.MenuItems
GetMenuItemsNameAndText(m)
Next
End If
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 依据菜单名称返回菜单对象
''' </summary>
''' <param name="FormIstance">窗体实例</param>
''' <param name="MenuItemName">菜单名称</param>
''' <returns>菜单</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [lzmtw] 2005-10-21 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function GetMenuItemByName(ByVal FormIstance As Form, ByVal MenuItemName As String) As MenuItem
Dim MenuItemFieldInfo As FieldInfo
MenuItemFieldInfo = FormIstance.GetType.GetField("_" & MenuItemName, FindBinding)
If MenuItemFieldInfo Is Nothing Then
Return Nothing
Else
Return CType(MenuItemFieldInfo.GetValue(FormIstance), MenuItem)
End If
End Function
''' -----------------------------------------------------------------------------
''' <summary>
''' 给出菜单对象返回菜单名称
''' </summary>
''' <param name="FormIstance">窗体实例</param>
''' <param name="MenuItem">菜单</param>
''' <returns>菜单名称</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [lzmtw] 2005-10-21 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function GetMenuItemName(ByVal FormIstance As Form, ByVal MenuItem As MenuItem) As String
Dim MenuItemFieldInfo As FieldInfo
For Each MenuItemFieldInfo In FormIstance.GetType.GetFields(FindBinding)
If MenuItemFieldInfo.GetValue(FormIstance) Is MenuItem Then
Return MenuItemFieldInfo.Name.Substring(1)
End If
Next
Return String.Empty
End Function
End Class
End Namespace
Imports System.Reflection
Namespace LzmTW.Common
''' -----------------------------------------------------------------------------
''' Project : CommonTest
''' Class : LzmTW.Common.MenuItemClass
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' 菜单操作
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [lzmtw] 2005-10-21 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class MenuItemClass
Private Const FindBinding As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public
Private Shared tmpHashtable As New Hashtable
Private Shared tmpForm As Form
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回主菜单所有菜单项的名称和标题
''' </summary>
''' <param name="FormIstance">窗体实例</param>
''' <param name="MainMenu">主菜单</param>
''' <returns>名称和标题列表</returns>
''' <remarks>
''' 对应于哈希表的键和值
''' </remarks>
''' <history>
''' [lzmtw] 2005-10-21 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function GetMenuItemsNameAndText(ByVal FormIstance As Form, ByVal MainMenu As MainMenu) As Hashtable
tmpForm = FormIstance
tmpHashtable.Clear()
For Each o As MenuItem In MainMenu.MenuItems
GetMenuItemsNameAndText(o)
Next
Return tmpHashtable
End Function
Private Shared Sub GetMenuItemsNameAndText(ByVal oMenuItem As MenuItem)
tmpHashtable.Add(GetMenuItemName(tmpForm, oMenuItem), oMenuItem.Text)
If oMenuItem.IsParent Then
For Each m As MenuItem In oMenuItem.MenuItems
GetMenuItemsNameAndText(m)
Next
End If
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 依据菜单名称返回菜单对象
''' </summary>
''' <param name="FormIstance">窗体实例</param>
''' <param name="MenuItemName">菜单名称</param>
''' <returns>菜单</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [lzmtw] 2005-10-21 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function GetMenuItemByName(ByVal FormIstance As Form, ByVal MenuItemName As String) As MenuItem
Dim MenuItemFieldInfo As FieldInfo
MenuItemFieldInfo = FormIstance.GetType.GetField("_" & MenuItemName, FindBinding)
If MenuItemFieldInfo Is Nothing Then
Return Nothing
Else
Return CType(MenuItemFieldInfo.GetValue(FormIstance), MenuItem)
End If
End Function
''' -----------------------------------------------------------------------------
''' <summary>
''' 给出菜单对象返回菜单名称
''' </summary>
''' <param name="FormIstance">窗体实例</param>
''' <param name="MenuItem">菜单</param>
''' <returns>菜单名称</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [lzmtw] 2005-10-21 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function GetMenuItemName(ByVal FormIstance As Form, ByVal MenuItem As MenuItem) As String
Dim MenuItemFieldInfo As FieldInfo
For Each MenuItemFieldInfo In FormIstance.GetType.GetFields(FindBinding)
If MenuItemFieldInfo.GetValue(FormIstance) Is MenuItem Then
Return MenuItemFieldInfo.Name.Substring(1)
End If
Next
Return String.Empty
End Function
End Class
End Namespace