简体   繁体   English

使用键盘上的Enter作为计算器相同

[英]Use Enter on keyboard as equal for calculator

When I try to use the Enter key as equal on the keyboard, it always enters the number that was recently pressed on screen. 当我尝试在键盘上使用Enter键时,它总是输入最近在屏幕上按下的数字。 I would like to use the Enter on the keyboard as equal and not enter in the number that was recently pressed on the screen by the user. 我想使用键盘上的Enter键相等,而不是输入用户最近在屏幕上按下的数字。

I am writing a calculator in C# with Visual Studio Community 2013. 我正在使用Visual Studio Community 2013在C#中编写计算器。

public calculatorForm()
{
        InitializeComponent();
}

// Manages the numeracy and period buttons
private void mathematicalButtons_Click(object sender, EventArgs e)
{
        if ((calculatorResults.Text == "0")||(operationPressed))
            calculatorResults.Clear();

        operationPressed = false;

        Button a = (Button)sender;

        if (a.Text == ".")
        {
            if(!calculatorResults.Text.Contains("."))
                calculatorResults.Text = calculatorResults.Text + a.Text;
        }
        else
            calculatorResults.Text = calculatorResults.Text + a.Text;            
}

// Manages the addition, subtract, multiplication, and divide buttons
private void operatorButtons_Click(object sender, EventArgs e)
{
        Button a = (Button)sender;

        if (value != 0)
        {
            equalButton.PerformClick();
            operationPressed = true;
            operation = a.Text;
            equationLabel.Text = value + " " + operation;
        }
        else
        {
            operation = a.Text;
            value = double.Parse(calculatorResults.Text);
            operationPressed = true;
            equationLabel.Text = value + " " + operation;
        }
}

// Manages the clear button
private void clearButton_Click(object sender, EventArgs e)
{
        calculatorResults.Text = "0";
        value = 0;
        equationLabel.Text = "";
}

// Manages the clear entry button
private void clearEntryButton_Click(object sender, EventArgs e)
{
        calculatorResults.Text = "0";
}

// Manages the equal button
private void equalButton_Click(object sender, EventArgs e)
{
        equationLabel.Text = "";

        switch (operation)
        {
            case "+":
                calculatorResults.Text = (value + Double.Parse(calculatorResults.Text)).ToString();
                break;

            case "-":
                calculatorResults.Text = (value - Double.Parse(calculatorResults.Text)).ToString();
                break;

            case "*":
                calculatorResults.Text = (value * Double.Parse(calculatorResults.Text)).ToString();
                break;

            case "/":
                calculatorResults.Text = (value / Double.Parse(calculatorResults.Text)).ToString();
                break;

            default:
                break;
        }

        value = Double.Parse(calculatorResults.Text);
        operation = "";
}

// Allows user to use computer keyboard to enter data
private void calculatorForm_KeyPress(object sender, KeyPressEventArgs e)
{
        switch (e.KeyChar.ToString())
        {
            case "0":
                zeroButton.PerformClick();
                break;

            case "1":
                oneButton.PerformClick();
                break;

            case "2":
                twoButton.PerformClick();
                break;

            case "3":
                threeButton.PerformClick();
                break;

            case "4":
                fourButton.PerformClick();
                break;

            case "5":
                fiveButton.PerformClick();
                break;

            case "6":
                sixButton.PerformClick();
                break;

            case "7":
                sevenButton.PerformClick();
                break;

            case "8":
                eightButton.PerformClick();
                break;

            case "9":
                nineButton.PerformClick();
                break;

            case ".":
                periodButton.PerformClick();
                break;

            case "/":
                divideButton.PerformClick();
                break;

            case "*":
                multiplicationButton.PerformClick();
                break;

            case "-":
                subtractButton.PerformClick();
                break;

            case "+":
                additionButton.PerformClick();
                break;

            case "=":
                equalButton.PerformClick();
                break;

            default:
                break;
        }
    }
}

The problem here is that when you press the enter key, the control with focus grabs the event. 这里的问题是当你按下回车键时,带焦点的控件会抓住事件。 When you click a button, you give it the focus, so the next time you click enter that button will grab the event and in the case of a button the enter key is interpreted as the button being pressed. 单击某个按钮时,会为其指定焦点,因此下次单击“输入”时,该按钮将抓取该事件,如果是按钮,则输入键将被解释为按下按钮。

What you want is for the form to always have focus, that way you know the key events will always go to the form and not any other control (such as a button). 你想要的是表单始终具有焦点,这样你就知道关键事件将始终转到表单而不是任何其他控件(例如按钮)。 In order to do that, look at the answers to this question: C# Stop Button From Gaining Focus on Click 为了做到这一点,请看看这个问题的答案: C#停止按钮从获得焦点点击

If you need any more help, just let me know! 如果您需要更多帮助,请告诉我们!

btn_equalButton.Focus();

Put this in after ever button click and it will just change the focus to equals. 点击按钮后将其放入,只需将焦点更改为等于。 When you click the enter key it will preform the click event of the button it is focused on. 当您单击回车键时,它将执行它所关注的按钮的单击事件。

Also TabStop = false on all controls Setting tab index to -1 also works 所有控件上的TabStop = false设置tab index to -1也可以

This is so that pressing tab wont move the focus 这样按压标签不会移动焦点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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