none
if else RRS feed

  • Question

  • Who can tell why a simple construct does not work in UWP:

    private void kd_NumberInputValidation(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
            {
                int Z1 = (int)e.Key;
    
                if (!(Z1 >= 48 &&  Z1<=57))
                {
                    n_AllowableInternalPressure_P.Text = ((int)e.Key).ToString();
                }
    
                else if (Z1==190)
                {
                    n_AllowableInternalPressure_P.Text = "rt";
                }
    
                else
                {
    
                }
            }
    This test code catches which button on the keyboard the user presses and writes the ASCII code to the n_AllowableInternalPressure_P field. But there were some problems:
    why in the field when the "point" is pressed, the value 190 appears (should 46) and why when the "point" itself is pressed (value 190), control does not go to the second "if" - "rt" should be written in the field.

    Sunday, October 25, 2020 3:58 PM

All replies

  • Who can tell why a simple construct does not work in UWP:

    private void kd_NumberInputValidation(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
            {
                int Z1 = (int)e.Key;
    
                if (!(Z1 >= 48 &&  Z1<=57))
                {
                    n_AllowableInternalPressure_P.Text = ((int)e.Key).ToString();
                }
    
                else if (Z1==190)
                {
                    n_AllowableInternalPressure_P.Text = "rt";
                }
    
                else
                {
    
                }
            }
    This test code catches which button on the keyboard the user presses and writes the ASCII code to the n_AllowableInternalPressure_P field. But there were some problems:
    why in the field when the "point" is pressed, the value 190 appears (should 46) and why when the "point" itself is pressed (value 190), control does not go to the second "if" - "rt" should be written in the field.

    I can't see why you would get 190 instead of 46 when you press the period key
    on the keyboard. However, it's clear why you don't execute the code in the
    first else block. That code will only get executed when the first if() is false. 
    The if() gets executed when the key code is NOT between 48 and 57.
    So the associated else code only gets executed when the key code IS between
    48 and 57, and obviously neither 190 nor 46 qualify.

    - Wayne

    Sunday, October 25, 2020 7:01 PM
  • Maybe the keyboard language (layout) was changed to non-English language?

    In order to intercept 190, try putting ‘if( Z1 == 190)’ before other ifs.

    Sunday, October 25, 2020 7:05 PM

  • This test code catches which button on the keyboard the user presses and writes the ASCII code to the n_AllowableInternalPressure_P field. But there were some problems:
    why in the field when the "point" is pressed, the value 190 appears (should 46) 

    What device are you using?

    What font are you using?

    Does the keyboard have optional or alternate characters for some keys?

    For example, does it have a key to show the Three Quarters fraction symbol?
    That symbol appears to be 190 (0xBE) in many fonts.

    - Wayne

    Sunday, October 25, 2020 7:14 PM