1

I have scenarios that don't want the end user to input Unicode control characters to the Windows.Forms.TextBox, as you may know, if you right click a TextBox, a context menu will shown and the last menu item on this context menu is "Insert Unicode control character->", which can insert some Unicode control characters into the TextBox.

Is there anybody knows how to disable or hide these menus -> "Insert Unicode control character", "Show Unicode control characters".

svick
  • 236,525
  • 50
  • 385
  • 514
Joboy
  • 101
  • 2
  • 7
  • You can disable the context menu all together by using `textBox1.ContextMenu = new ContextMenu();` if that's an option. – Bala R Jul 05 '11 at 15:27

3 Answers3

1

Even if you disable this context menu entry, the user can still enter all kinds of strange characters using copy & paste or using Alt-Numpad. If you want to restrict your input strictly to, say, A-Z, you can use a MaskedTextBox control.

If you need more fine-grained control, you can handle the TextBox.KeyPress Event. An example of this technique can be found in the following SO question:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
1

Override your text box ContextMenu.

Design you own ContextMenu and implement only the functionality you want on it. and then assign that contextMenu to your textBox: myTextBox.ContextMenu = myContextMenu;

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
1

To add to Jalal's answer,

var contextMenu = new ContextMenu();
contextMenu.MenuItems.Add(new MenuItem("Copy", (s, ea) => textBox1.Copy()));
contextMenu.MenuItems.Add(new MenuItem("Paste", (s, ea) => textBox1.Paste()));
contextMenu.MenuItems.Add(new MenuItem("Undo", (s, ea) => textBox1.Undo()));
contextMenu.MenuItems.Add(new MenuItem("Select All", (s, ea) => textBox1.SelectAll()));
....

textBox1.ContextMenu = contextMenu;
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Thanks for you solution. I have couple of datagridview and textboxes, replacing everything concerns me a bit. But I have the same app in vb where these menus are not displayed by default. – Joboy Jul 05 '11 at 17:53