如何以编程方式添加菜单 [英] how to add menu programmically

查看:93
本文介绍了如何以编程方式添加菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建此代码以编程方式添加菜单及其子菜单
现在我想在该菜单及其子菜单之后创建另一个菜单
以及如何在菜单中添加另一个子菜单我的意思是说我已经在子菜单上创建了,我想在第一个子菜单下创建另外两个子菜单

i create this code to add menu and its sub menu programmically
now i want to create another menu afer that menu and its sub menu
and how to add another sub menu into the menu i mean to say that i have already created on sub menu now i want to create another two sub menus under the first sub menu

public void menu_Function(string firstitme, string newitem)
        {
            Add_New_Form form1 = new Add_New_Form();
            mnuMain = new MenuStrip();
            Controls.Add(mnuMain);
            mnuFile = new ToolStripMenuItem(firstitme);
            mnuFileNew = new ToolStripMenuItem(newitem);
            mnuFile.DropDownItems.Add(mnuFileNew);
            mnuMain.Items.Add(mnuFile);
        }

推荐答案

您不需要此行:对您没有任何帮助!
You don''t need this line: it does nothing useful for you at all!
Add_New_Form form1 = new Add_New_Form();


之后,您需要进一步考虑.
我要进行的第一个更改是:


After that it needs more thinking about before yo go any further.
The first change I would make is:

public void menu_Function(string firstitme, string newitem)
    {
    if (mnuMain == null)
        {
        mnuMain = new MenuStrip();
        Controls.Add(mnuMain);
        }
    mnuFile = new ToolStripMenuItem(firstitme);
    mnuFileNew = new ToolStripMenuItem(newitem);
    mnuFile.DropDownItems.Add(mnuFileNew);
    mnuMain.Items.Add(mnuFile);
    }

这样可以防止每次调用例程时都添加一个新的菜单栏-您只希望在应用程序中添加一个菜单栏!
可能您想对mnuFile等执行类似的操作,但是我不确定您要实现哪种菜单结构.

您要做的最好的事情可能是创建一个新项目,并在设计器中构建一个菜单结构,该结构类似于您尝试以编程方式创建的菜单结构.如果您随后查看VS设计器代码,它将向您展示Microsoft如何做到这一点.然后,您可以对其进行调整,使其更加灵活并可以在现有项目中按需工作-全部包含在< MyFormName> .designer.cs文件中.

This prevents a new menu strip being added each time you call the routine - you only want one in your App!
Probably, you want to do something similar for your mnuFile and so forth, but I am not absolutely clear what kind of menu structure you are trying to achieve.

The best thing for you to do is probably to create a new project, and in the designer build a menu structure similar to that you are trying to create programatically. If you then look at the VS designer code for this, it will show you how Microsoft do it. you can then adapt that to be more flexible and work as you want in your existing project - it is all in the <MyFormName>.designer.cs file.


Griff的解决方案:问自己一个问题如何以非编程方式添加菜单 ?".没有这样的东西!

即使您使用Designer,您所做的所有工作都是以编程方式"完成的;当您添加控件并修改控件的属性时,只会为您自动生成一些代码.

因此,这是一个简单的秘诀:如果您不知道如何以编程方式进行操作,但是知道如何使用Designer,只需使用它并查看生成的代码,了解它的工作原理,然后利用这种理解来动态使用这些控件在运行时.

—SA
In addition to the correct solution to Griff: ask yourself a question "how to add menu not programmatically?". There is no such thing!

Even if you use Designer, all you do is done "programmatically"; Designed only auto-generate some code for you as you add controls and modify control their property.

So here is a simple recipe: if you don''t know how to do things programmatically but know how to use Designer, simply use it and look at the generated code, learn how it works and later use this understanding for using those controls dynamically during run time.

—SA


这篇关于如何以编程方式添加菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆