How to use the KeyPress event
-
Hello, I am a newbie at C#. I am trying to make a text box only allow alpha characters, such as a - z upper and lower case, the backspace, and space keys. How would i write that in the keypress event and how do i get it to activate on that text box? thanks any help will be appreciated.
-
Hello, I am a newbie at C#. I am trying to make a text box only allow alpha characters, such as a - z upper and lower case, the backspace, and space keys. How would i write that in the keypress event and how do i get it to activate on that text box? thanks any help will be appreciated.
Something like this should get you started:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Allow only alpha characters
base.OnKeyPress(e);
if (Char.IsLetter(e.KeyChar) || Char.IsPunctuation(e.KeyChar)
|| Char.IsSeparator(e.KeyChar) || Char.GetNumericValue(e.KeyChar) == -1)
e.Handled = false;
else
e.Handled = true;
}-Nick Parker
-
Something like this should get you started:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Allow only alpha characters
base.OnKeyPress(e);
if (Char.IsLetter(e.KeyChar) || Char.IsPunctuation(e.KeyChar)
|| Char.IsSeparator(e.KeyChar) || Char.GetNumericValue(e.KeyChar) == -1)
e.Handled = false;
else
e.Handled = true;
}-Nick Parker
well thanks for the help anyways but it doesn't work. i can still type numbers and wierd characters into the text box. Does anyone else have any ideals?here is what my teacher showed us in class but i can't get anything out of it? thanks private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar < 'a'||e.KeyChar > 'z')&& e.KeyChar != '\b') e.Handled = true; }
-
Hello, I am a newbie at C#. I am trying to make a text box only allow alpha characters, such as a - z upper and lower case, the backspace, and space keys. How would i write that in the keypress event and how do i get it to activate on that text box? thanks any help will be appreciated.
-
well thanks for the help anyways but it doesn't work. i can still type numbers and wierd characters into the text box. Does anyone else have any ideals?here is what my teacher showed us in class but i can't get anything out of it? thanks private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar < 'a'||e.KeyChar > 'z')&& e.KeyChar != '\b') e.Handled = true; }
kharr1027 wrote: private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar < 'a'||e.KeyChar > 'z')&& e.KeyChar != '\b') e.Handled = true; } A few things, first of all I just re-tried the code example I gave you and it does work, however I noticed in the code example you listed you have
Form1_KeyPress
which appears that you have an event handler registered for theKeyPress
event of aForm
, not aTextBox
. Make sure whatever the name of your textbox that you add to your form includes the following event/delegate assignment (for this example the name of my textbox is simply the default oftextBox1
):this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
then the following will work to only allow alphabetic characters, space and backspace entries:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Allow only alpha characters
base.OnKeyPress(e);
if (Char.IsLetter(e.KeyChar) || Char.IsPunctuation(e.KeyChar)
|| Char.IsSeparator(e.KeyChar) || Char.GetNumericValue(e.KeyChar) == -1)
e.Handled = false;
else
e.Handled = true;
}-Nick Parker