简体   繁体   中英

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. 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.

I am writing a calculator in C# with Visual Studio Community 2013.

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

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

This is so that pressing tab wont move the focus

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