如何更改(以及稍后恢复)文本框中的默认上下文菜单

发布于 2024-09-14 18:52:33 字数 225 浏览 2 评论 0原文

我想更改默认的文本框上下文菜单,所以我创建了自己的菜单,并像这样分配了它们

texbox.ContextMenu = myContextMenu

,但是我不知道如何恢复默认的文本框菜单(在运行时)。仅当我用鼠标右键单击文本框(同时按住 Control 按钮)时,我才需要显示 myContextMenu。在其他情况下,我需要显示默认的文本框上下文菜单。 是否可以 ??

I wanted to change default textbox context menu, so I created my own menu and them I assigned it like that

texbox.ContextMenu = myContextMenu

However I don't know how to restore default textbox menu (in a runtime). I need myContextMenu to show only when I click textbox with right mouse button (while holding Control button). In other cases I need default textbox contextmenu to show.
Is it possible ??

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

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

发布评论

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

评论(3

傲鸠 2024-09-21 18:52:33

以下是微软给出的示例:

http://msdn.microsoft.com/en -us/library/ms750420.aspx

郑重声明,以下是使用 WinForms 执行此操作的方法:

public partial class TextBoxContextMenuDemo : Form
{
    ContextMenu mnuContextDefault;
    ContextMenu mnuContextAlt;

    MenuItem mnuItmAltMenuTest;

    public TextBoxContextMenuDemo()
    {
        InitializeComponent();
        InitializeAltContextMenu();
    }

    private void InitializeAltContextMenu()
    {
        mnuContextDefault = new ContextMenu();
        mnuContextDefault = this.TextBox1.ContextMenu;

        mnuItmAltMenuTest = new MenuItem();
        mnuItmAltMenuTest.Index = -1;
        mnuItmAltMenuTest.Text = "Test Menu Item";
        mnuItmAltMenuTest.Click += new System.EventHandler(this.mnuItmAltMenuTest_Click);

        mnuContextAlt = new ContextMenu();
        mnuContextAlt.MenuItems.Add(mnuItmAltMenuTest);
    }

    private void TextBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            if ((Control.ModifierKeys == Keys.Control))
            {
                this.TextBox1.ContextMenu = mnuContextAlt;
                TextBox1.ContextMenu.Show(TextBox1, new Point(e.X, e.Y));
            }
            else
            {
                this.TextBox1.ContextMenu = mnuContextDefault;
            }
        }
    }

    private void mnuItmAltMenuTest_Click(object sender, System.EventArgs e)
    {
        MessageBox.Show("You clicked the alternate test menu item!");
    }
}

HTH!

Here is the example given by Microsoft:

http://msdn.microsoft.com/en-us/library/ms750420.aspx

For the record, here is the way to do this using WinForms:

public partial class TextBoxContextMenuDemo : Form
{
    ContextMenu mnuContextDefault;
    ContextMenu mnuContextAlt;

    MenuItem mnuItmAltMenuTest;

    public TextBoxContextMenuDemo()
    {
        InitializeComponent();
        InitializeAltContextMenu();
    }

    private void InitializeAltContextMenu()
    {
        mnuContextDefault = new ContextMenu();
        mnuContextDefault = this.TextBox1.ContextMenu;

        mnuItmAltMenuTest = new MenuItem();
        mnuItmAltMenuTest.Index = -1;
        mnuItmAltMenuTest.Text = "Test Menu Item";
        mnuItmAltMenuTest.Click += new System.EventHandler(this.mnuItmAltMenuTest_Click);

        mnuContextAlt = new ContextMenu();
        mnuContextAlt.MenuItems.Add(mnuItmAltMenuTest);
    }

    private void TextBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            if ((Control.ModifierKeys == Keys.Control))
            {
                this.TextBox1.ContextMenu = mnuContextAlt;
                TextBox1.ContextMenu.Show(TextBox1, new Point(e.X, e.Y));
            }
            else
            {
                this.TextBox1.ContextMenu = mnuContextDefault;
            }
        }
    }

    private void mnuItmAltMenuTest_Click(object sender, System.EventArgs e)
    {
        MessageBox.Show("You clicked the alternate test menu item!");
    }
}

HTH!

远山浅 2024-09-21 18:52:33

实际上,这比乍看起来更难做到。我相信默认上下文菜单是控件实际模板的一部分。

如果您只想剪切/复制/粘贴,最简单的方法是创建第二个上下文菜单来实现这些选项。如果这样做,您可以使用内置的 ApplicationCommands 来实现 not不仅仅是功能,还要自动本地化这个ContextMenu。

It would actually be more difficult to do than it would first seem. I believe that the default context menu is part of the actual template of the control.

The simplest approach, if you only want Cut/Copy/Paste, is to create a second ContextMenu implementing those options. If you do, you can use the built in ApplicationCommands to implement not only the functionality, but also to automatically localize this ContextMenu.

祁梦 2024-09-21 18:52:33

您可以将 ContextMenu-Property 设置为 null。 OnContextMenuOpening 事件也可以帮助您。

You could just set the ContextMenu-Property to null. Also the OnContextMenuOpening event can help you.

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