简体   繁体   中英

Why are numeric keystrokes appearing twice?

I'm new to C#. Using the code below, whenever I press a number key on my keyboard, it will display twice in the textbox. When I press "1" on the keyboard it will display "11", and when I press "2" it will display "22". Why is this?

private void Window_TextInput(object sender, TextCompositionEventArgs e)
{

    if(!isNumeric(e.Text))
    {
        string display = string.Empty;
        display += e.Text;

        displayNum(display);
    }
    else
    {
        String inputOperator = string.Empty;
        inputOperator += e.Text;

        if (inputOperator.Equals("+"))
        {
            ApplySign(sign.addition, "+");
        }

    }
}

private bool isNumeric(string str)
{
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[^0-9]");
    return reg.IsMatch(str);
}

private void window_keyUp(object sender, KeyEventArgs e)
{
    if (e.Key >= Key.D0 && e.Key <= Key.D9)
    {
        int num = e.Key - Key.D0;
        outputText2.Text += num;
    }

}

private void BtnNum_Click(object sender, RoutedEventArgs e)
{
    Button num = ((Button)sender);
    displayNum(num.Content.ToString());
}
private void displayNum(String n)
    {
        if (operator1 == 0 && double.Parse(n) == 0)
        {

        }
        else
        {

            if (operator1 == 0)
            {
                outputText2.Clear();
            }
            outputText2.Text += n;

            operator1 = double.Parse(outputText2.Text);
            outputText2.Text = Convert.ToString(operator1);         
        }

    }

You have two events that are handeling the Keyboard events. Although not really sure what the displayNum() method is doing

I am assuming the Window_TextInput event is the event you wish to primarily handle the event.

Try adding

e.Handled = true;

In the Window_TextInput method. If that doesn't solve the problem can you post the displayNum() method?

EDIT:

After further review of the code and trying the same I do not see the relevance for the window_keyUp method as your Window_TextInput handles the input characters and has more applicable logic for handling the TextInput changes.

After I removed the window_keyUp event method the output appeared as expected (although commented out the ApplySign() method.

You've subscribed to two window-level text-related events - TextInput and KeyUp - and both of them end up appending input to the TextBox.

  • window_keyUp appends numbers to the TextBox

  • It looks like Window_TextInput is supposed to append non-numeric characters, but your RegEx is incorrect ( [^0-9] matches anything that is not numeric, so IsNumeric returns True if the input is not a number)

The effect is that every numeric key press shows up twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM