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 开发者_如何学Go"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".
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:
- How do I make a textbox that only accepts numbers?
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;
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;
精彩评论