简体   繁体   English

使用WindowsForm在C#中使用SubMenu?

[英]SubMenu in C# using WindowsForm?

I have a menustrip consists of Menu and Tools 我有一个包含菜单和工具的菜单条

in "Menu" i have subMenus like msO1,msO2,msO3......., and on "Tools" i have subMenus like msP1,msP2,msP3......., 在“菜单”中,我有子菜单,如msO1,msO2,msO3 .......,在“工具”中,我有子菜单,如msP1,msP2,msP3 .......,

on Form load all the subMenus visible is false..., On button Click user want to select which subMenus he want..., 在窗体上加载所有可见的子菜单为false ...,在按钮上单击用户想要选择他想要的子菜单...,

in the textBox(txtSelect) if user enter 1,3..., he get msO1, msO3....., 在textBox(txtSelect)中,如果用户输入1,3 ...,他得到msO1,msO3 .....,

my code is a hardcode..., if i have 20 subMenus means this code is not helpfull anybody have an idea..., 我的代码是一个硬代码...,如果我有20个子菜单,则意味着此代码无济于事,任何人都有一个主意...,

   private void btnSelect_Click_1(object sender, EventArgs e)
    {
        msO1.Visible = false;//msO1 is a submenu
        msO2.Visible = false;
        msO3.Visible = false;
        msP1.Visible = false;
        msP2.Visible = false;
        msP3.Visible = false;
        string word = txtSelect.Text;
        string[] splt = word.Split(',');
        int[] arrayItms = new int[splt.Length];
        for (int x = 0; x < splt.Length; x++)
        {
            arrayItms[x]=Convert.ToInt32(splt[x].ToString());
            if (splt.Length > 0)
            {
                switch (arrayItms[x])
                {
                    case 1:
                        msO1.Visible = true; break;
                    case 2:
                        msO2.Visible = true; break;
                    case 3:
                        msO3.Visible = true; break;
                    case 4:
                        msP1.Visible = true; break;
                    case 5:
                        msP2.Visible = true; break;
                    case 6:
                        msP3.Visible = true; break;
                }
            }
        }

    }

Create an array of your MenuStrip 创建一个MenuStrip数组

MenuStrip[] mstrip = new MenuStrip[] 
{
    msO1,msO2, msO3, msP1, msP2, msP3 // add other menus here when needed
};

now you could work on the array as a Whole to make visible or not your menus 现在您可以将阵列作为一个整体来显示或不显示菜单

   for(int x = 0; x < menus.Length; x++)
      mstrip[x].Visible = false;

and your code could be simplified with 并且您的代码可以简化为

    for (int x = 0; x < splt.Length; x++)
    {
        int menuIndex;
        if(Int32.TryParse(splt[x], out menuIndex))
        {
            menuIndex--;
            if(menuIndex >= 0 && menuIndex < mstrip.Length)
                mstrip[menuIndex].Visible = true;
       }
    }

Remember, arrays indexes start at zero (while your user will probably start counting a 1). 请记住,数组索引从零开始(而您的用户可能会开始从1开始计数)。

Loop through each ToolStripMenuItem control in the menu strip items and set them to visible. 循环浏览菜单条项目中的每个ToolStripMenuItem控件,并将它们设置为可见。 You can add further conditions inside the loop to define which of the menu items should be made visible based on the users choice.. 您可以在循环内添加其他条件,以根据用户的选择来定义应显示哪些菜单项。

        foreach (ToolStripMenuItem mi in menuStrip1.Items)
        {
            mi.Visible = true;
        }

You could use something like this 你可以用这样的东西

    string word = txtSelect.Text;
    string[] splt = word.Split(',');
    for (int x = 0; x < splt.Length; x++)
    {
       Control myControl1 = FindControl("ms" + splt[x]);
       if ( myControl1 != null )
         (ToolStripMenuItem)myControl1.Visible = true;
    }

Untested but this should get you half way there I hope. 未经测试,但是我希望这能使您完成一半。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM