Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中启用和禁用ToolStripMenu项?_C#_Arrays_Foreach_Toolstripmenu - Fatal编程技术网

在C#中启用和禁用ToolStripMenu项?

在C#中启用和禁用ToolStripMenu项?,c#,arrays,foreach,toolstripmenu,C#,Arrays,Foreach,Toolstripmenu,我有一个表单,它包含一个菜单,有两个条目,即菜单和工具。这两个菜单都有一些子菜单 现在我有一个名为txtSelect的文本框和一个名为btnVisible的按钮,如果我在文本框中输入1,2,菜单中的子菜单应该不可见。我写了下面的代码,它是硬写的 ToolStripMenuItem[] mstrip = new ToolStripMenuItem[] { msO1, msO2, msO3, msP1, msP2, msP3 }; if (txtSelect.Text.Length > 2)

我有一个
表单
,它包含一个
菜单
,有两个条目,即菜单和工具。这两个菜单都有一些
子菜单

现在我有一个名为
txtSelect
文本框
和一个名为
btnVisible
按钮
,如果我在
文本框
中输入1,2,
菜单
中的
子菜单
应该不可见。我写了下面的代码,它是硬写的

ToolStripMenuItem[] mstrip = new ToolStripMenuItem[] { msO1, msO2, msO3, msP1, msP2, msP3 };
if (txtSelect.Text.Length > 2)
{
    string word = txtSelect.Text;
    string[] splt = word.Split(',');
    for (int x = 0; x < mstrip.Length; x++)
        mstrip[x].Visible = true;
    for (int x = 0; x < splt.Length; x++)
    {
        int y = Convert.ToInt32(splt[x].ToString()) - 1;
        if (y >= 0 && y < mstrip.Length)
            mstrip[y].Visible = false;
        textBox1.AppendText(mstrip[y].Text);
        textBox2.AppendText(mstrip[y].OwnerItem.Text);
    }
}

也许你想要的是:

List<Int32> lstindex = new List<Int32>();
String[] splt = txtSelect.Text.Split(',');

// initialize list of indexed for textbox
foreach (String str in splt)
{
    lstindex.Add(Convert.ToInt32(str) - 1);
}

// for each menu
foreach (ToolStripMenuItem mnItem in msMenus.Items)
{
     // for each menu item
    foreach (ToolStripItem item in mnItem.DropDown.Items)
    {
        // if index of item is in the list of indexed, set visible to false, otherwise to true
        item.Visible = lstindex.Contains(mnItem.DropDown.Items.IndexOf(item)) ? false : true;
    }
}
List lstinex=new List();
字符串[]splt=txtSelect.Text.Split(',');
//初始化文本框的索引列表
foreach(splt中的字符串str)
{
添加(转换为32(str)-1);
}
//对于每个菜单
foreach(msMenus.Items中的ToolStripMenuItem mnItem)
{
//对于每个菜单项
foreach(mnItem.DropDown.Items中的ToolStripItem项)
{
//如果项的索引在索引列表中,则将visible设置为false,否则设置为true
item.Visible=lstindex.Contains(mnItem.DropDown.Items.IndexOf(item))?false:true;
}
}

您能澄清一下这个问题吗?你需要它做什么?我想用foreach循环进行上述验证…我不想硬编码…,我想所有菜单的子项都应该按顺序…,比如1,2,3,4,5,6,7,8,9,10。。。。。。。。。。。。,
List<Int32> lstindex = new List<Int32>();
String[] splt = txtSelect.Text.Split(',');

// initialize list of indexed for textbox
foreach (String str in splt)
{
    lstindex.Add(Convert.ToInt32(str) - 1);
}

// for each menu
foreach (ToolStripMenuItem mnItem in msMenus.Items)
{
     // for each menu item
    foreach (ToolStripItem item in mnItem.DropDown.Items)
    {
        // if index of item is in the list of indexed, set visible to false, otherwise to true
        item.Visible = lstindex.Contains(mnItem.DropDown.Items.IndexOf(item)) ? false : true;
    }
}