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.