How to create a non-selectable context menu item?

2019-07-07 06:07发布

问题:

I have a simple context menu. And I would like to add a title item to it, which cannot be selected, not even highlighted with the mouse cursor. When I set Enabled = false; I still can mark it and it feels stupid because it's obviously disabled and the text is gray.


Example:

Like this. I can't mark or select the "Menu" item. It must never be blue. So I want this in my C# application. Simple, no styles.


Test code:

public Form1()
{
    ContextMenuStrip = new ContextMenuStrip();
    ContextMenuStrip.Font = new Font("Arial", 8);
    ToolStripItem a = ContextMenuStrip.Items.Add("--- Title ---");
    a.Enabled = false;
    a.Font = new Font("Consolas", 16, FontStyle.Bold | FontStyle.Italic);
    ContextMenuStrip.Items.Add("Alice");
    ContextMenuStrip.Items.Add("Bob");
    ContextMenuStrip.Items.Add("Conrad");
}

回答1:

I think you want to add a ToolStripLabel item to your strip, like this:

ContextMenuStrip.Items.Insert(0, new ToolStripLabel("--- Title ---"));

This should add a label which serves as a marker, and should not show anything when the mouse moves over it.

(There's a similar answer here, which covers the same point.)