日期:2009-08-04 浏览次数:20443 次
Namespace Microsoft.Samples.WinForms.VB.Menus
Public Class Menus
Inherits System.WinForms.Form
Private components As System.ComponentModel.Container
Private label1 As System.WinForms.Label
Private mainMenu As System.WinForms.MainMenu
Private label1ContextMenu As System.WinForms.ContextMenu
Private Class FontSizes
public shared Small As Single = 8f
public shared Medium As Single = 12f
public shared Large As Single = 24f
End Class
'Font face and size
Private fontFace As String = "Arial"
Private fontSize As Single = FontSizes.Medium
'Used to track which menu items are checked/unchecked
Private mmiArial As MenuItem
Private mmiTimesNewRoman As MenuItem
Private mmiCourier As MenuItem
Private mmiSmall As MenuItem
Private mmiMedium As MenuItem
Private mmiLarge As MenuItem
Private cmiArial As MenuItem
Private cmiTimesNewRoman As MenuItem
Private cmiCourier As MenuItem
Private cmiSmall As MenuItem
Private cmiMedium As MenuItem
Private cmiLarge As MenuItem
Private miMainFormatFontChecked As MenuItem
Private miMainFormatSizeChecked As MenuItem
Private miContextFormatFontChecked As MenuItem
Private miContextFormatSizeChecked As MenuItem
Public Sub New()
MyBase.New
' Required by the Windows Forms Designer
InitializeComponent
' TODO: Add any constructor code after InitializeComponent call
label1.Font = new Font(fontFace, fontSize)
'Add File Menu
Dim miFile As MenuItem = mainMenu.MenuItems.Add("&File")
miFile.MenuItems.Add(new MenuItem("&Open...", new EventHandler(AddressOf Me.FileOpen_Clicked), Shortcut.CtrlO))
miFile.MenuItems.Add("-") ' Gives us a seperator
miFile.MenuItems.Add(new MenuItem("E&xit", new EventHandler(AddressOf Me.FileExit_Clicked), Shortcut.CtrlX))
'Add Format Menu
Dim miFormat As MenuItem = mainMenu.MenuItems.Add("F&ormat")
'Font Face sub-menu
mmiArial = new MenuItem("&Arial", AddressOf Me.FormatFont_Clicked)
mmiArial.Checked = true
mmiArial.DefaultItem = true
mmiTimesNewRoman = new MenuItem("&Times New Roman", AddressOf Me.FormatFont_Clicked)
mmiCourier = new MenuItem("&Courier New", AddressOf Me.FormatFont_Clicked)
miFormat.MenuItems.Add( "Font &Face" _
, new EventHandler(AddressOf Me.FormatFont_Clicked) _
, (new MenuItem(){ mmiArial, mmiTimesNewRoman, mmiCourier }))
'Font Size sub-menu
mmiSmall = new MenuItem("&Small", AddressOf Me.FormatSize_Clicked)
mmiMedium = new MenuItem("&Medium", AddressOf Me.FormatSize_Clicked)
mmiMedium.Checked = true
mmiMedium.DefaultItem = true
mmiLarge = new MenuItem("&Large", AddressOf Me.FormatSize_Clicked)
miFormat.MenuItems.Add( "Font &Size" _
, new EventHandler(AddressOf Me.FormatSize_Clicked) _
, (new MenuItem(){ mmiSmall, mmiMedium, mmiLarge }))
'Add Format to label context menu
'Note have to add a clone because menus can't belong to 2 parents
label1ContextMenu.MenuItems.Add(miFormat.CloneMenu)
' Set up the context menu items - we use these to check and uncheck items
cmiArial = label1ContextMenu.MenuItems(0).MenuItems(0).MenuItems(0)
cmiTimesNewRoman = label1ContextMenu.MenuItems(0).MenuItems(0).MenuItems(1)
cmiCourier = label1ContextMenu.MenuItems(0).MenuItems(0).MenuItems(2)
cmiSmall = label1ContextMenu.MenuItems(0).MenuItems(1).MenuItems(0)
cmiMedium = label1ContextMenu.MenuItems(0).MenuItems(1).MenuItems(1)
cmiLarge = label1ContextMenu.MenuItems(0).MenuItems(1).MenuItems(2)
'We use these to track which menu items are