none
Check condition with ascii code? RRS feed

  • Question

  • Hello,

    I the below code if condition checked with ascii code?

    Thanks

    private void keypress_event(object sender, KeyPressEventArgs e) { Button b = new Button(); b.Text = e.KeyChar.ToString(); if (e.KeyChar >= '0' && e.KeyChar <= '9') // check with ascii code?

    The condition check keychar shoud be between 1 to 9(number) { btnAllnum_click(b, null); } }Word 2007



    • Edited by Arash_89 Saturday, January 20, 2018 1:00 AM
    Saturday, January 20, 2018 12:59 AM

All replies

  • To compare the KeyChar, which is of type char, with something else which is not a char (such as an integer ascii code), you first have to convert one of the two pieces of information to the type of the other, i.e., either convert the KeyChar to int and then compare to the ascii code, or convert the ascii code to char and then compare to the KeyChar:

    if (((int)(e.KeyChar)) >= 48 && ((int)(e.KeyChar)) <= 57) ...

    - or -

    if (e.KeyChar >= (char)48 && e.KeyChar <= (char)57) ...

    • Proposed as answer by Bob Ding Monday, January 22, 2018 8:59 AM
    Saturday, January 20, 2018 10:44 AM
    Moderator
  • This code runs how you state it. 

    So what is your problem?


    Success Cor

    • Proposed as answer by Bob Ding Monday, January 22, 2018 9:04 AM
    Sunday, January 21, 2018 10:19 AM