Winform主菜单两种实现方式-1.)从工具箱中双击MenuStrip控件,窗体的顶部就会放置一个MenuStrip控件,或者在工具箱中单击MenuStrip控件,拖至窗体中也可以. 2.)在MenuStrip的“Type Here”输入主菜单和菜单名字,热键用(&X)表示. 用快捷键选择主菜单,可以用Alt + 预定义的按键来实现,子菜单的选择只需要按定义的热键即可. 3.)双击File*菜单下面的子菜单,创建子菜单Open、Save、Exit的单击事件代码: private void OpenOToolStripMenuItem_Click(object sender, EventArgs e) {     OpenFileDialog ofd = new OpenFileDialog     {         //初始化打开对话框的路径         InitialDirectory = @"D:",         Title = "Open F

时间:2024-03-21 13:17:45

private void FormUI_Load(object sender, EventArgs e)

{

    //实例化一个MenuStrip对象

    MenuStrip myMenuStrip = new MenuStrip();

    //实例化*菜单"文件","帮助"

    ToolStripMenuItem menu_file = new ToolStripMenuItem("文件(&F)");

    //ToolStripMenuItem menu_help = new ToolStripMenuItem("帮助(&H)");

    //实例化文件菜单的子菜单"打开""保存"

    ToolStripMenuItem menuitem_open = new ToolStripMenuItem("打开(&O)");

    ToolStripMenuItem menuitem_save = new ToolStripMenuItem("保存(&S)");

    menuitem_open.Click += OpenOToolStripMenuItem_Click;

    menuitem_save.Click += SaveSToolStripMenuItem_Click;

    //"打开""保存"子菜单添加到文件*菜单下面

    menu_file.DropDownItems.AddRange(new ToolStripMenuItem[] { menuitem_open, menuitem_save });

    //将*菜单"文件"添加到MenuStrip

    myMenuStrip.Items.Add(menu_file);

    //MenuStrip添加到窗体中

    this.Controls.Add(myMenuStrip);

}

子菜单单击事件实现方式和控件方式一样,没有区别, 不再累述..

相关文章