keypress problem

T

Thomas Wang

I have a sample:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((int)e.KeyChar<255)
e.Handled = true;
}

above code can prevent ASCII code when I pressed.

but I wonder how I can prevent Chinese characters Key in ? because:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((int)e.KeyChar>255)
e.Handled = true;
}
can not work properly when I key in. What can i do ?

B.Rgds
 
M

Magnus Krisell

I have a sample:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((int)e.KeyChar<255)
e.Handled = true;
}

above code can prevent ASCII code when I pressed.

Actually, there are only 128 ASCII characters.
but I wonder how I can prevent Chinese characters Key in ? because:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((int)e.KeyChar>255)
e.Handled = true;
}
can not work properly when I key in. What can i do ?

I would suggest using one of the static methods in the System.Char class,
for example:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsLetter(e.KeyChar))
e.Handled = true;
}


This will block all Unicode characters that are classified as Letter. There
are
also IsDigit(), IsPunctuation() and so on.

- Magnus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top