如何阻止 ContextMenuStrip 中的项目专门处理 & 符号?

发布于 2024-07-27 00:35:36 字数 257 浏览 3 评论 0原文

我有一个 ContextMenuStrip ,它显示可以由用户命名的项目; 允许用户给出包含&符号的项目名称。 当显示 ContextMenuStrip 时,项目将 & 符号视为转义序列,并为下一个字符添加下划线。

在设置项目的 Text 成员之前,我可以将所有 & 符号加倍,但该成员在代码中的其他地方使用,因此如果可能的话,我想阻止 ContextMenuStrip 专门处理 & 符号。 有没有办法关闭这种行为?

I have a ContextMenuStrip which displays items which can be named by the user; the user is allowed to give the items names containing ampersands. When the ContextMenuStrip is shown, the items' treat the ampersands as escape sequences, and underline the next character.

I could double up all the ampersands before I set the items' Text members, but that member is used elsewhere in the code so if possible, I'd like to stop the ContextMenuStrip from treating ampersands specially. Is there a way to turn that behaviour off?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年已逝 2024-08-03 00:35:36

使用 && 显示单个 &

编辑: 抱歉,我错过了您问题的第二部分:(

您始终可以使用设置时在文本上使用 string.Replace("&", "&&") ,但这看起来很混乱。

另一种选择是从 ToolStripMenuItem 继承。 > 并覆盖文本属性集,将 & 替换为 && ,这样会好一点,因为它会将代码保留在一个位置。

use && to display a single &

Edit: Sorry, I missed the second part of your question :(

You could always use string.Replace("&", "&&") on the text when you set it, but that seems messy.

Another alternative would be to inherit from ToolStripMenuItem and override the Set of the Text Property to replace & with &&. That would be a little better as it would keep the code in once place.

羁〃客ぐ 2024-08-03 00:35:36

不幸的是,我认为没有任何内置支持(例如 Button 控件的 UseMnemonic 属性)。 一种方法是使用强力方法,该方法将遍历表单上的控件树并对找到的所有 ToolStripMenuItem 执行替换:

public TheForm()
{

    InitializeComponent();
    FixAmpersands(this.Controls);
}

private static void FixAmpersands(Control.ControlCollection controls)
{
    foreach (Control control in controls)
    {
        if (control is ToolStrip)
        {
            FixAmpersands((control as ToolStrip).Items);
        }
        if (control.Controls.Count > 0)
        {
            FixAmpersands(control.Controls);
        }
    }
}

private static void FixAmpersands(ToolStripItemCollection toolStripItems)
{
    foreach (ToolStripItem item in toolStripItems)
    {
        if (item is ToolStripMenuItem)
        {
            ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
            tsmi.Text = tsmi.Text.Replace("&", "&&");
            if (tsmi.DropDownItems.Count > 0)
            {
                FixAmpersands(tsmi.DropDownItems);
            }
        }                
    }
}

当然,如果菜单结构不是动态且偶尔构建的,这主要是有用的在表单的生命周期内。 如果时不时地添加新项目,那么您可能需要通过某种方法来运行它们,该方法将在单个项目上执行&符号加倍。

I don't think that there is any built in support for that (like the UseMnemonic property of the Button control), unfortunately. One way to do it is to make a brute-force method that will walk the control tree on the form and perform a replace on all ToolStripMenuItems found:

public TheForm()
{

    InitializeComponent();
    FixAmpersands(this.Controls);
}

private static void FixAmpersands(Control.ControlCollection controls)
{
    foreach (Control control in controls)
    {
        if (control is ToolStrip)
        {
            FixAmpersands((control as ToolStrip).Items);
        }
        if (control.Controls.Count > 0)
        {
            FixAmpersands(control.Controls);
        }
    }
}

private static void FixAmpersands(ToolStripItemCollection toolStripItems)
{
    foreach (ToolStripItem item in toolStripItems)
    {
        if (item is ToolStripMenuItem)
        {
            ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
            tsmi.Text = tsmi.Text.Replace("&", "&&");
            if (tsmi.DropDownItems.Count > 0)
            {
                FixAmpersands(tsmi.DropDownItems);
            }
        }                
    }
}

This is, of course, useful primarily if the menu structure is not build up dynamically and occasionaly during the lifetime of the form. If new items are added every now and then you will probably need to run them through some method that will perform the ampersand-doubling at on a single item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文