只检查一个ToolStripMenuItem

时间:2012-11-28 11:09:38

标签: winforms checked toolstrip toolstripdropdown toolstripitem

我有ToolStripToolStripDropDownButton个,每个都有一组DropDownItems

当用户点击DropDownItem时,会显示复选标记。

enter image description here

默认情况下,可以单击多个项目,因此会出现多个复选标记。

enter image description here

我要做的是当用户点击一个DropDownItem时,应取消选中其他已检查的项目。换句话说,DropDown列表中应始终只有一个已检查项。

我已经讨论了一段时间了,但是当我取消选中其他项目时,我无法弄清楚如何保持当前检查的项目。

以下是我现在的代码。

private void subietm1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UncheckOtherToolStripMenuItems(sender);
        }

        public void UncheckOtherToolStripMenuItems(object selectedMenuItem)
        {
            List<ToolStripDropDownButton> dropdownButtons = new List<ToolStripDropDownButton>();
            foreach (ToolStripItem item in toolStrip1.Items)
            {
                if (item is ToolStripDropDownButton)
                {
                    dropdownButtons.Add((ToolStripDropDownButton)item);
                }
            }

            foreach (ToolStripDropDownButton btn in dropdownButtons)
            {
                foreach (ToolStripMenuItem d in btn.DropDownItems)
                {
                    if (d.Checked)
                        d.CheckState = CheckState.Unchecked;
                }
            }
        }

如果有人可以对此有所了解,或者告诉我一个简单的方法,我将不胜感激。

谢谢。

5 个答案:

答案 0 :(得分:12)

这么容易......

实施他们的方法,如下所述:

    private void subietm1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
    }

    public void UncheckOtherToolStripMenuItems(ToolStripMenuItem selectedMenuItem)
    {
        selectedMenuItem.Checked = true;

        // Select the other MenuItens from the ParentMenu(OwnerItens) and unchecked this,
        // The current Linq Expression verify if the item is a real ToolStripMenuItem
        // and if the item is a another ToolStripMenuItem to uncheck this.
        foreach (var ltoolStripMenuItem in (from object 
                                                item in selectedMenuItem.Owner.Items 
                                            let ltoolStripMenuItem = item as ToolStripMenuItem 
                                            where ltoolStripMenuItem != null 
                                            where !item.Equals(selectedMenuItem) 
                                            select ltoolStripMenuItem))
            (ltoolStripMenuItem).Checked = false;

        // This line is optional, for show the mainMenu after click
        selectedMenuItem.Owner.Show();
    }

一个细节是,您可以为所有单击menuItens实现相同的方法,为此将方法UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);的相同调用添加到每个ToolstripMenuItem的事件单击中,请将此示例添加到另外两个ToolstripMenuItens:

    private void subietm2ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
    }

    private void subietm3ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
    }

答案 1 :(得分:1)

我只是使用item_Click事件设置菜单中的所有项目,所以如果单击一个,那么它将只运行下面的代码。不需要为每个按钮设置一个事件。

    private void item_Click(object sender, EventArgs e)
    {
        // Set the current clicked item to item
        ToolStripMenuItem item = sender as ToolStripMenuItem;
        // Loop through all items in the subMenu and uncheck them but do check the clicked item
        foreach (ToolStripMenuItem tempItemp in (ToolStripMenuItem)item.OwnerItem.DropDownItems)
        {
            if (tempItemp == item)
                tempItemp.Checked = true;
            else
                tempItemp.Checked = false;
        }
    }

如果您想在运行期间将多个项目添加到列表中并以上述方式连接它们,则可以运行以下代码。

    private void subItemsMenus(ToolStripMenuItem parentItem, string[] listItems)
    {
        // Clear tool strip items first
        parentItem.DropDownItems.Clear();
        // Add items that are in the list
        foreach (string subMenuItem in listItems)
        {
            ToolStripMenuItem item = new ToolStripMenuItem();
            //Name that will appear on the menu
            item.Text = subMenuItem; 
            //Put in the Name property whatever necessary to retrieve your data on click event
            item.Name = subMenuItem;
            //On-Click event
            item.Click += new EventHandler(item_Click);
            //Add the submenu to the parent menu
            parentItem.DropDownItems.Add(item);
        }

答案 2 :(得分:1)

我有另一种方法可行:

每件商品都会转到ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e) 并且每个项目都有自己的标记1, 2, 3, 4, 5。 在开始时检查一项,并tag = 1

    int selecteditem = 1;
    bool atwork = false;
    private void dzienToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
    {
        if (atwork) return;
        else atwork = true;

        selecteditem = Convert.ToInt32(((ToolStripMenuItem)sender).Tag);

        foreach (ToolStripMenuItem it in sometooltipdropdown.DropDownItems)
        {
            if (Convert.ToInt32(it.Tag) != selecteditem)
            {
                it.Checked = false;
            }                
        }

        atwork = false;
    }

答案 3 :(得分:1)

最简单的方法是添加 DropDownItemClicked 事件并创建自己的方法:

private void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        if (e.ClickedItem != null)
        {
            CheckSelected((ToolStripDropDownButton)sender, e.ClickedItem);
        }
    }

    private void CheckSelected(ToolStripDropDownButton button, ToolStripItem selectedItem)
    {
        foreach (ToolStripMenuItem item in button.DropDownItems)
        {
            item.Checked = (item.Name == selectedItem.Name) ? true : false;
        }
    }

答案 4 :(得分:0)

您可以通过复制到数组然后使用扩展来获取计数。

ToolStripItem[] controls = new ToolStripItem[ToolStrip.DropDownItems.Count];
ToolStrip.DropDownItems.CopyTo(controls,0);
intcheckCount = controls.Count(c => (c as ToolStripMenuItem).Checked);

if (checkCount == 0) // must keep 1 selection
     item.Checked = true;
else if (checkCount > 1)  //uncheck all others
     controls.Cast<ToolStripMenuItem>().Where(c => c.Checked && c.Name != item.Name)
       .ToList().ForEach(s => s.Checked = false);