欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中怎样在ToolStripMenuItem下再添加子级菜单

程序员文章站 2022-04-10 13:49:55
场景 在右键菜单ContextMenuStrip下添加子菜单选项可以通过 ContextMenuStrip menuStrip ToolStripMenuItem mnuChartOption = new ToolStripMenuItem(); //新建菜单项对象 mnuChartOption.N ......

场景

在右键菜单contextmenustrip下添加子菜单选项可以通过

contextmenustrip menustrip
toolstripmenuitem mnuchartoption = new toolstripmenuitem();     //新建菜单项对象
mnuchartoption.name = "chart_option";
mnuchartoption.text = "图形选项";
//点击弹出图形选项对话框
mnuchartoption.click += delegate(object sender, eventargs e)
   {
                
    };
menustrip.items.add(mnuchartoption);

 

来实现。但是如果在右键菜单的子菜单选项下再添加子菜单选项,形成二级子菜单。

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载

实现

toolstripmenuitem 的子级菜单仍然是toolstripmenuitem ,主要通过dropdownitems属性来构建关联关系。

构建右键二级子菜单示例代码

     toolstripmenuitem mnudirectprint = new toolstripmenuitem("直接打印");
            mnudirectprint.name = "directprint";
            mnudirectprint.text = "直接打印";
            mnudirectprint.click += delegate(object sender, eventargs e)
            {
                control.doprint();
            };

            toolstripmenuitem mnuprintpageset = new toolstripmenuitem("打印页面设置");
            mnuprintpageset.name = "printpageset";
            mnuprintpageset.text = "打印页面设置";
            mnuprintpageset.click += delegate(object sender, eventargs e)
            {
                control.dopagesetup();
            };

            toolstripmenuitem mnuprint = new toolstripmenuitem();
            mnuprint.name = "mnuprint";
            mnuprint.text = "打印";
            mnuprint.dropdownitems.add(mnudirectprint);
            mnuprint.dropdownitems.add(mnuprintpageset);

            
            menustrip.items.add(mnuprint);

效果

C#中怎样在ToolStripMenuItem下再添加子级菜单

 

 

C#中怎样在ToolStripMenuItem下再添加子级菜单