4

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;
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

3

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!

Community
  • 1
  • 1
acernine
  • 729
  • 2
  • 6
  • 16
  • I was able to understand the that link, but am not sure where I should enter this.Focus(); ((Button)sender).Enabled = false; in my code. –  May 02 '15 at 18:49
  • You don't need to disable the button, so you only need the this.Focus(); part. You should put it at the end of any button click event. This way whenever a button is clicked and steals the focus from you, you force it to give it back to the form! – acernine May 02 '15 at 19:09
  • @acernine good call, I think the focus handling is the right way to go. – Philip Stuyck May 02 '15 at 19:09
  • @acernine - When I added this.Focus(); to my button click event it did not work unless I put it in the right spot. –  May 02 '15 at 19:13
  • To clarify, have you now found the 'right spot' - or in other words is it now working? – acernine May 02 '15 at 19:14
  • @acernine It is not working as I don't know where to add this.Focus();. –  May 02 '15 at 19:16
  • Thinking about it now, it may be easier to link all of your button's key_press events to 'calculatorForm_KeyPress'. Do this in the same way that you linked all of your buttons to one event. If you do this you wont need the this.Focus() – acernine May 02 '15 at 19:19
  • I rather do this.Focus(); I want to learn somthing new as I just started larning C# yesterday. –  May 02 '15 at 19:25
  • In that case you may want to look at this answer http://stackoverflow.com/a/17353023/3512689 It offers an interesting alternative solution which demonstrates inheritance from control objects nicely. – acernine May 02 '15 at 19:30
0
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

JhWOLFJh
  • 67
  • 2
  • 15