3

I've noticed something wierd with the WinForm TextBox's Context Menu. The TextBox control has a default context menu that has Cut, Copy, Paste and a few other things. I am trying to replace this menu with one of my own. I have created a simple test application with one form and one text box on the form and added the following code:

Form1()
{
    InitailizeComponent();
    ContextMenu menu = new ContextMenu();
    menu.MenuItems.Add("Hello World", HelloWorld_Clicked);
    textBox1.ContextMenu = menu;
}

private void HelloWorld_Clicked(object sender, EventArgs e)
{
    MessageBox.Show("Hello World!");
}

When I run this I can get my context menu to appear by right clicking the textbox and then releasing the mouse button without moving the mouse. However if I press the right mouse button over the textbox, hold it down, then move the mouse outside the textbox and finally relase the mouse button then I get the default text box context menu.

Is it possible to stop it doing this?

UPDATE: In case it makes a difference the system is running on Windows XP Pro SP3 & .Net 3.5.

UPDATE 2: I tried this again in .Net Core 6 on Windows 11 with the following code and got the same issue.

namespace WinFormsApp1;

public partial class Form1 : Form
{
    public Form1()
    {
        this.InitializeComponent();
        ContextMenuStrip menu = new ContextMenuStrip();
        menu.Items.Add("Hello World", null, HelloWorld_Clicked);
        textBox1.ContextMenuStrip = menu;
    }

    private void HelloWorld_Clicked(object? sender, EventArgs e)
    {
        MessageBox.Show("Hello World!");
    }
}

Given MS have now started using GitHub I have raised an issue there.

Martin Brown
  • 24,692
  • 14
  • 77
  • 122

3 Answers3

1

I suspect that releasing the mouse button outside the text box opens a context menu for the form instead of the text box. I haven't tested this, it's just a guess. You might be able set a context menu for the form as well with code like this, but I haven't tried it myself:

Form1()
{
    InitilizeComponent();
    ContextMenu menu = new ContextMenu();
    menu.MenuItems.Add("Hello World", HelloWorld_Clicked);
    textBox1.ContextMenu = menu;
    this.ContextMenu = menu;
}

private void HelloWorld_Clicked(object sender, EventArgs e)
{
    MessageBox.Show("Hello World!");
}
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • I tried your fix and I'm afraid that you suspect wrong. The menu that comes up would make no sense for the form anyway. – Martin Brown Oct 31 '12 at 16:51
  • Sorry to hear that, @MartinBrown. [This answer](http://stackoverflow.com/a/2910287/4794) discusses modal loops on native controls that might explain the behaviour you see. I suspect fixing it will be more hassle than it's worth. – Don Kirkby Oct 31 '12 at 19:16
  • Are you placing your code in the "New" method of either the parent form for your textbox or in the "New" method of your textbox if the textbox is part of a User Defined Control? Perhaps other code in your project is changing the textbox property "ContextMenu" at some stage. – Chris Raisin May 31 '22 at 01:15
1

One thing you can try:

[DllImport("user32.dll")]
public static extern bool ReleaseCapture();

void textBox1_MouseDown(object sender, MouseEventArgs e) {
  if (e.Button == MouseButtons.Right) {
    ReleaseCapture();
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
0

If it helps, I found this answer in another part of the Internet:

[C#] textBox1.ContextMenu = new ContextMenu();

[VB.Net] textBox1.ContextMenu = New ContextMenu()

Chris Raisin
  • 384
  • 3
  • 7
  • I was already setting the context menu property to a new ContextMenu so that's not it. I just tried it again with the .Net Core's new ContextMenuStrip and that seems to have the same issue. – Martin Brown May 29 '22 at 20:08
  • Are you placing your code in the "New" method of either the parent form for your textbox or in the "New" method of your textbox if the textbox is part of a User Defined Control? Perhaps other code in your project is changing the textbox property "ContextMenu" at some stage. – Chris Raisin May 31 '22 at 01:16
  • I have literally put the entire program code in the example in my question. I'm not sure I understand what you mean by the "New" method? Are you referring to the constructor? If so yes I'm putting the code in the constructor of the form. – Martin Brown Jun 02 '22 at 18:59