0

MenuToolsの 2 つのエントリを持つをForm含む があります。2 つのメニューにはいくつかの.MenuSubMenus

これで、 と がTextBox呼び出されました。に 1,2 を入力すると、の は表示されません。私は次のコードを書きましたが、少しハードに書かれています。 txtSelectButtonbtnVisibleTextBoxSubMenuMenu

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、次のように試みましたが、結果は上記のコードと同じではありません。

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;
    }    
}
4

1 に答える 1

0

さて、あなたは次のようなものが欲しいかもしれません:

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;
    }
}
于 2013-03-18T10:04:21.013 に答える