1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace StandardMenu 9 { 10 class StandardMenu:Form 11 { 12 MenuItem miFileOpen, miFileSave; 13 MenuItem miEditCut, miEditCopy, miEditPaste; 14 15 //for popup code 16 bool bDocumentPresent = true; 17 bool bNonNullSelection = false; 18 bool bStuffInClipboard = true; 19 20 static void Main(string[] args) 21 { 22 Application.Run(new StandardMenu()); 23 } 24 public StandardMenu() 25 { 26 Text = "Standard Menu!"; 27 Menu = new MainMenu(); 28 29 //File 30 MenuItem mi = new MenuItem("&File"); 31 mi.Popup += new EventHandler(MenuFileOnPopup); 32 Menu.MenuItems.Add(mi); 33 int index = Menu.MenuItems.Count - 1; 34 35 //测试index 36 //在File处,index值是1 37 Console.WriteLine(Menu.MenuItems.Count); 38 39 //Fiel Open 40 miFileOpen = new MenuItem("&Open..."); 41 miFileOpen.Click += new EventHandler(miFileOpen_Click); 42 miFileOpen.Shortcut = Shortcut.CtrlO; 43 Menu.MenuItems[index].MenuItems.Add(miFileOpen); 44 45 //File Save 46 miFileSave = new MenuItem("&Save"); 47 miFileSave.Click += new EventHandler(miFileSave_Click); 48 miFileSave.Shortcut = Shortcut.CtrlS; 49 Menu.MenuItems[index].MenuItems.Add(miFileSave); 50 51 //Horizontal Line 52 mi = new MenuItem("-"); 53 Menu.MenuItems[index].MenuItems.Add(mi); 54 55 //File Exit 56 mi = new MenuItem("&E&xit"); 57 mi.Click += new EventHandler(miFileEdit_Click); 58 Menu.MenuItems[index].MenuItems.Add(mi); 59 60 //Edit 61 mi = new MenuItem("&Edit"); 62 mi.Popup +=new EventHandler(MenuEditOnPopup); 63 Menu.MenuItems.Add(mi); 64 index = Menu.MenuItems.Count - 1; 65 66 //测试index 67 //在Edit处,index值是2 68 Console.WriteLine(Menu.MenuItems.Count); 69 70 //Edit Cut 71 miEditCut = new MenuItem("Cu&t"); 72 miEditCut.Click += new EventHandler(miEditCut_Click); 73 miEditCut.Shortcut = Shortcut.CtrlX; 74 Menu.MenuItems[index].MenuItems.Add(miEditCut); 75 76 //Edit Copy 77 miEditCopy = new MenuItem("&Copy"); 78 miEditCopy.Click += new EventHandler(miEditCopy_Click); 79 miEditCopy.Shortcut = Shortcut.CtrlC; 80 Menu.MenuItems[index].MenuItems.Add(miEditCopy); 81 82 //Edit Paste 83 miEditPaste = new MenuItem("&Paste"); 84 miEditPaste.Click += new EventHandler(miEditPaste_Click); 85 miEditPaste.Shortcut = Shortcut.CtrlV; 86 Menu.MenuItems[index].MenuItems.Add(miEditPaste); 87 88 // Help 89 mi = new MenuItem("&Help"); 90 Menu.MenuItems.Add(mi); 91 index = Menu.MenuItems.Count - 1; 92 93 //测试index 94 //在Help处,index值是3 95 Console.WriteLine(Menu.MenuItems.Count); 96 97 // Help About 98 mi = new MenuItem("&About StandarMenu..."); 99 mi.Click +=new EventHandler(MenuHelpAboutOnClick); 100 Menu.MenuItems[index].MenuItems.Add(mi); 101 } 102 103 void MenuEditOnPopup(object sender, EventArgs e) 104 { 105 miEditCut.Enabled = bNonNullSelection; 106 miEditCopy.Enabled = bNonNullSelection; 107 miEditPaste.Enabled = bStuffInClipboard; 108 } 109 110 void MenuHelpAboutOnClick(object sender, EventArgs e) 111 { 112 MessageBox.Show("This should be a File About box!", Text); 113 } 114 115 void miEditPaste_Click(object sender, EventArgs e) 116 { 117 MessageBox.Show("This should be a File Edit Paste dialog box!", Text); 118 } 119 120 void miEditCopy_Click(object sender, EventArgs e) 121 { 122 MessageBox.Show("This should be a File Edit Save dialog box!", Text); 123 } 124 125 void miEditCut_Click(object sender, EventArgs e) 126 { 127 MessageBox.Show("This should be a File Edit Cut dialog box!", Text); 128 } 129 130 void miFileEdit_Click(object sender, EventArgs e) 131 { 132 Close(); 133 } 134 135 void miFileSave_Click(object sender, EventArgs e) 136 { 137 MessageBox.Show("This should be a File Save dialog box!", Text); 138 } 139 140 void miFileOpen_Click(object sender, EventArgs e) 141 { 142 MessageBox.Show("This should be a File Open dialog box!",Text); 143 } 144 145 void MenuFileOnPopup(object sender, EventArgs e) 146 { 147 miFileSave.Enabled = bDocumentPresent; 148 } 149 } 150 }
以上代码中禁用了“复制”和“剪切”菜单,所以从下图可以看出禁用的实际效果 。
还有,代码中的Menu.MenuItems.Count是随着主菜单项的加入,逐渐增加的;
标签:index,Menu,MenuItems,mi,new,禁用,菜单项,Click,winform From: https://www.cnblogs.com/chenlight/p/16863391.html