如何在单击某个项目后使 ToolStripMenu 保持打开状态

Chi*_*era 5 c# user-interface menu menuitem winforms

我正在创建一个ToolStripMenu如下所示的内容,它应该允许用户与“XML”和“非 XML”项目进行交互,就好像它们是表单上的常规复选框一样。但是,当选中/取消选中一项时,菜单将关闭。如何在不关闭菜单的情况下允许选中/取消选中某个项目?或者是否有不同的标准方法来实现相同的行为?

在此输入图像描述

所以我想要的是能够单击“非 XML”,显示一个复选框并使菜单保持打开状态。这个想法是,最后一个菜单项将是“完成”,当单击它时,“G2S”子项将保持打开状态,但“显示”子项(XML,非 XML)将关闭。

有任何想法吗?

注意:我知道这可能不是最好的用户界面设计。然而,我想知道如何实现这一点,只是为了获得一些有关处理菜单的技术知识。

Neo*_*isk 5

Stackoverflow 上的这个线程描述了有趣的概念:

这是已接受答案的实质:

ParentMenu.DropDown.AutoClose = false;
Run Code Online (Sandbox Code Playgroud)

它完全符合您的要求 - 单击子项目时防止菜单关闭。


Chi*_*era 2

Posted in case somebody finds it helpful.

Instead of trying to do exactly what I had originally intended, I've come up with the following:

1- Use a ContextMenuStrip
2- When the user clicks on the ToolStripMenu item I display the ContextMenuStrip at a location near the menu item as shown below: ( note the positioning still needs adjusting )

在此输入图像描述

为了实现此功能,我ContextMenuStrip在运行时构建了代码,以便ContextMenuStrip可以根据情况动态构建其中的项目。

代码片段:

ContextMenuStrip单击菜单项时显示:

private void filterToolStripMenuItem_Click(object sender, EventArgs e)
{
    contextMenuStrip1.Show(this, 180, 20);
}
Run Code Online (Sandbox Code Playgroud)

构建ContextMenuStrip

    if (protInfo.Name == "QCOM" )
    {
        BroadCast = new CheckBox();
        BroadCast.Text = "Date/Time Broadcast";
        BroadCast.Checked = FlagSet(CurrentFilter, (Byte)Filter.DateTimeBC);
        ToolStripControlHost Ch1 = new ToolStripControlHost(BroadCast);

        GenPoll = new CheckBox();
        GenPoll.Text = "Status Poll";
        GenPoll.Checked = FlagSet(CurrentFilter, (Byte)Filter.GenStatusPoll);
        ToolStripControlHost Ch2 = new ToolStripControlHost(GenPoll);

        GenPollResp = new CheckBox();
        GenPollResp.Text = "Status Poll Response";
        GenPollResp.Checked = FlagSet(CurrentFilter, (Byte)Filter.GenStatusResponse);
        ToolStripControlHost Ch3 = new ToolStripControlHost(GenPollResp);

        Button btnDone = new Button();
        btnDone.Text = "Done";
        ToolStripControlHost Ch4 = new ToolStripControlHost(btnDone);
        btnDone.Click += new EventHandler(btnDone_Click);

        contextMenuStrip1.Items.Clear();
        contextMenuStrip1.Items.Add(Ch1);
        contextMenuStrip1.Items.Add(Ch2);
        contextMenuStrip1.Items.Add(Ch3);
        contextMenuStrip1.Items.Add(Ch4);
        contextMenuStrip1.Enabled = true;
        filterToolStripMenuItem.Enabled = true;
    }
    else
    {
        filterToolStripMenuItem.Enabled = false;
    }
Run Code Online (Sandbox Code Playgroud)

这可能不是最好的用户界面设计,但它似乎有效。