首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ToolStripMenu中启用和禁用C#条目?

在ToolStripMenu中启用和禁用C#条目?
EN

Stack Overflow用户
提问于 2013-03-18 09:22:09
回答 1查看 535关注 0票数 0

我有一个Form,它包含有两个条目的Menu,即菜单和工具。这两种方法都有一些SubMenus

现在我有一个名为TextBoxtxtSelect和一个名为btnVisibleButton,如果我在TextBox中输入了1,2,那么Menu中的SubMenu是不可见的。我写了下面的代码,它是硬写的.

代码语言:javascript
复制
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);
    }
}

我希望在foreach单击事件中使用Button循环,并尝试使用以下方法,但是结果与上面的代码不一样。

代码语言:javascript
复制
foreach (ToolStripMenuItem mnItem in msMenus.Items)
{
    MessageBox.Show(mnItem.Text);
    for (int i = 0; i < mnItem.DropDown.Items.Count; i++)
    {
        MessageBox.Show(mnItem.DropDown.Items[i].Text);
        mnItem.DropDown.Items[i].Visible = true;
    }    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-18 10:04:21

嗯,也许你想要的是:

代码语言:javascript
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15473279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档