如何创建不可选择的上下文菜单项? [英] How to create a non-selectable context menu item?

查看:69
本文介绍了如何创建不可选择的上下文菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的上下文菜单。而且我想向其中添加一个标题项,该项无法选择,甚至不能用鼠标光标突出显示。当我设置 Enabled = false; 时,我仍然可以对其进行标记,这很愚蠢,因为它显然已被禁用并且文本为灰色。



示例:

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:

就像这样。我无法标记或选择菜单项。它一定不能是蓝色的。所以我想在我的C#应用​​程序中使用它。简单,没有样式。

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.



测试代码:

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");
}


推荐答案

我想添加一个 ToolStripLabel 项添加到您的条中,例如:

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.)

这篇关于如何创建不可选择的上下文菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆