Search code examples
c#.netwinformscontextmenustrip

C# Disable default windows contextstrip from within a textbox


I have disabled the default context strip throughout my form, and replaced with my own. Within my contextmenustrip i have a toolstriptextbox, however when right-clicking within this it brings up the default windows context menu.. Thanks in advance for any and all replies, Regards, Dave


Solution

  • Easiest way to remove the default right-click menu is to define your own (empty) menu:

    myTextBox.ContextMenuStrip = new ContextMenuStrip();
    

    This will, in one line, remove the default Windows right-click options. Understand that the keyboard shortcuts (Ctrl-C, Ctrl-X, Ctrl-V, Ctrl-A, Ctrl-Z) will all still work; you're not disabling the basic functionality, just the mouse-able access to it.

    For a TextBox within a ContextMenu of another TextBox, it's a little trickier, but basically you can directly access the TextBox that is contained within the ToolStripItem, using the item's TextBox property. This property is a vanilla System.Windows.Forms.TextBox, like any other you'd have in your app:

    myToolStripTextBox.TextBox.ContextMenuStrip = new ContextMenuStrip();