日期:2011-02-28  浏览次数:20572 次

MainMenu Merge

windows form的程序使用菜单mainmenu是,有时需要根据操作隐藏或显示menuitem,另外窗体类和关联的菜单项事件在一个class中,结构清晰。

do like this:
class Form1: Form{    
    public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();            
            IsMdiContainer = true;
            Menu = new MainMenu();
            MenuItem File = Menu.MenuItems.Add("&File");
            File.MergeType = MenuMerge.MergeItems;
            File.MergeOrder = 0;
            MenuItem FileNew = File.MenuItems.Add("&New", new EventHandler(New_Click));
            FileNew.Shortcut = Shortcut.CtrlN;
            FileNew.MergeOrder = 10;

            
        }
        private void New_Click(Object Sender,EventArgs e)
        {
        
        }
// other codes
}
public class doc : System.Windows.Forms.Form
    {
        public doc()
        {
            Menu = new MainMenu();
            MenuItem File = Menu.MenuItems.Add("&File");
            File.MergeType = MenuMerge.MergeItems ;
            File.MergeOrder = 0;
            MenuItem Save=File.MenuItems .Add ("&Save",new EventHandler (Save_Click));
            Save.MergeType =MenuMerge.MergeItems ;
            Save.MergeOrder =9;

            MenuItem Saveas=File.MenuItems .Add ("&Save as",new EventHandler (Saveas_Click));
            Saveas.MergeType =MenuMerge.MergeItems ;
            Saveas.MergeOrder =11;
            
            MenuItem Help = Menu.MenuItems.Add("&Help");
            Help.MergeOrder = 400;    
            Help.MenuItems.Add("&About .NET Resourcer");
        }
        private void Save_Clic