keypress problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Wang

    #1

    keypress problem

    I have a sample:

    private void textBox1_KeyPre ss(object sender, KeyPressEventAr gs 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_KeyPre ss(object sender, KeyPressEventAr gs e)
    {
    if ((int)e.KeyChar >255)
    e.Handled = true;
    }
    can not work properly when I key in. What can i do ?

    B.Rgds


  • Magnus Krisell

    #2
    Re: keypress problem

    > I have a sample:[color=blue]
    >
    > private void textBox1_KeyPre ss(object sender, KeyPressEventAr gs e)
    > {
    > if ((int)e.KeyChar <255)
    > e.Handled = true;
    > }
    >
    > above code can prevent ASCII code when I pressed.[/color]

    Actually, there are only 128 ASCII characters.
    [color=blue]
    >
    > but I wonder how I can prevent Chinese characters Key in ? because:
    >
    > private void textBox1_KeyPre ss(object sender, KeyPressEventAr gs e)
    > {
    > if ((int)e.KeyChar >255)
    > e.Handled = true;
    > }
    > can not work properly when I key in. What can i do ?
    >[/color]

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

    private void textBox1_KeyPre ss(object sender, KeyPressEventAr gs 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


    Comment

    Working...