In a C# 2010 desktop application, I want a textbox field to contain only letters in upper case. In addition, I want to allow for numbers. Any other values are not valid for this textbox field.
Thus can you show me code on how to setup the edit for uppercase letters and numbers only in the textbox?
Loading
Iftikar HussainPosted Jul 7, 2013, 1:53 AM
You can use Leave event of the textbox to check your input. It work for both copy paste and normal typing.
this.textBox1.Leave += new System.EventHandler(this.textBox1_TextLeave);
Regards,
Iftikar
VulpesPosted Jul 6, 2013, 6:47 PM
http://www.asciitable.com/
dcPosted Jul 6, 2013, 5:09 PM
Can you tell me where I can look to see what the various values of e.KeyCar means?
dcPosted Jul 6, 2013, 5:03 PM
Iftikar HussainPosted Jul 4, 2013, 2:04 AM
Regards,
Iftikar
VulpesPosted Jul 3, 2013, 4:49 PM
However, the easiest way to deal with this is to disable pasting altogether either from the context menu (by right clicking the TextBox) or from the keyboard by pressing either Control-V or Shift-Insert.
This can be done as follows:
public Form1()
{
InitializeComponent();
textBox1.ContextMenu = new ContextMenu(); // effectively disables context menu
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Control && e.KeyCode == Keys.V) || (e.Shift && e.KeyCode == Keys.Insert))
{
e.SuppressKeyPress = true;
}
}
Regarding Iftikar's approach (i.e. post entry validation), the regular expression is not quite right. It should be:
if (Regex.IsMatch(text, @"^[A-Z0-9]+$"))
If you want to allow the TextBox to be left blank, replace + with *
dcPosted Jul 3, 2013, 4:18 PM
dcPosted Jul 3, 2013, 4:15 PM
How would yu change the code?
Iftikar HussainPosted Jul 3, 2013, 12:57 PM
You can do this by using Regular Expression
Valid - A123, ABC1, ABC111, ABC3783782, AOEIS292999
Invalid - abc, 123, abc123, Abc123
Regards,
Iftikar
VulpesPosted Jul 3, 2013, 12:56 PM