9

我有一个ToolStrip带有多个ToolStripDropDownButtons 的,每个都有一组DropDownItems.

当用户单击 DropDownItem 时,将显示复选标记。

在此处输入图像描述

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

在此处输入图像描述

我想要做的是当用户单击一个 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;
                }
            }
        }

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

谢谢你。

4

5 回答 5

16

太简单...

如下所述实现他们的方法:

    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 的 Event click 中,请参见另外两个 ToolstripMenuItens 的此示例:

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

    private void subietm3ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
    }
于 2012-11-28T11:51:38.670 回答
3

我只是使用 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);
        }
于 2014-09-12T04:58:15.587 回答
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;
        }
    }
于 2020-04-17T08:10:47.550 回答
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;
    }
于 2017-03-11T21:52:48.930 回答
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);
于 2014-12-17T23:08:27.340 回答