Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用键盘上的Enter键作为计算器的等号_C#_Visual Studio 2013_Keyboard - Fatal编程技术网

C# 使用键盘上的Enter键作为计算器的等号

C# 使用键盘上的Enter键作为计算器的等号,c#,visual-studio-2013,keyboard,C#,Visual Studio 2013,Keyboard,当我尝试使用键盘上的Enter键作为equal时,它总是输入屏幕上最近按下的数字。我想使用键盘上的Enter键作为相等键,而不是输入用户最近在屏幕上按下的数字 我正在用Visual Studio Community 2013的C#语言编写一个计算器 public calculatorForm() { InitializeComponent(); } // Manages the numeracy and period buttons private void mathemati

当我尝试使用键盘上的Enter键作为equal时,它总是输入屏幕上最近按下的数字。我想使用键盘上的Enter键作为相等键,而不是输入用户最近在屏幕上按下的数字

我正在用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;
        }
    }
}

这里的问题是,当您按enter键时,具有焦点的控件将捕获事件。当你点击一个按钮时,你会给它焦点,所以下次你点击回车时,该按钮会抓取事件,如果是一个按钮,回车键会被解释为按钮被按下

您希望窗体始终具有焦点,这样您就知道关键事件将始终转到窗体,而不是任何其他控件(如按钮)。为此,请查看此问题的答案:

如果你需要更多的帮助,请告诉我

btn_equalButton.Focus();
把这个放进“以后”按钮点击,它只会将焦点更改为相等。当您单击enter键时,它将执行它所关注的按钮的单击事件

在所有控件上也是
TabStop=false
将选项卡索引设置为-1也有效


这是为了按tab键不会移动焦点

使用keydown事件。稍微复杂一点。对于非字符键(如enter键),也会出现一些关于KeyDown事件的文档:KeyDown和KeyUp。在询问之前,您搜索了什么?我能够理解该链接,但不确定应该在何处输入该链接。Focus();((按钮)发送器).Enabled=false;在我的代码中,您不需要禁用该按钮,因此只需要this.Focus();部分你应该把它放在任何按钮点击事件的末尾。这样,无论何时单击按钮并从您那里窃取焦点,您都会强制它将焦点返回表单@acernine呼叫很好,我认为焦点处理是正确的方式。@acernine-当我添加这个时。focus();对于我的按钮点击事件,除非我把它放在正确的位置,否则它不起作用。为了澄清,你现在找到“正确的位置”了吗?或者换句话说,它现在起作用了吗?