【问题标题】:Use Enter on keyboard as equal for calculator使用键盘上的 Enter 等同于计算器
【发布时间】:2015-05-02 17:48:50
【问题描述】:

当我尝试使用键盘上的 Enter 键时,它总是输入最近在屏幕上按下的数字。我想使用键盘上的 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;
        }
    }
}

【问题讨论】:

  • 使用 keydown 事件。稍微复杂一点。
  • KeyDown 事件的一些文档:msdn.microsoft.com/fr-fr/library/…
  • KeyDown 和 KeyUp,也为非字符键引发,例如 enter。
  • 问之前你搜索了什么?

标签: c# visual-studio-2013 keyboard


【解决方案1】:

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

您希望表单始终具有焦点,这样您就知道关键事件将始终转到表单而不是任何其他控件(例如按钮)。为此,请查看此问题的答案:C# Stop Button From Gaining Focus on Click

如果您需要更多帮助,请告诉我!

【讨论】:

  • 我能够理解那个链接,但不确定我应该在哪里输入 this.Focus(); ((Button)sender).Enabled = false;在我的代码中。
  • 你不需要禁用按钮,所以你只需要 this.Focus();部分。您应该将它放在任何按钮单击事件的末尾。这样,每当单击按钮并从您那里窃取焦点时,您就会强制它将其返回给表单!
  • @acernine 好电话,我认为焦点处理是正确的方法。
  • @acernine - 当我添加 this.Focus();对于我的按钮单击事件,除非我把它放在正确的位置,否则它不起作用。
  • 澄清一下,您现在是否找到了“正确的位置”——或者换句话说,它现在工作了吗?
【解决方案2】:
btn_equalButton.Focus();

在单击按钮后将其放入,它只会将焦点更改为等于。当你点击回车键时,它会执行它所关注的按钮的点击事件。

所有控件上还有TabStop = false 设置tab index to -1 也可以

这样按tab就不会移动焦点了

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 2010-11-02
    • 2011-11-25
    • 2019-11-19
    • 1970-01-01
    • 2013-04-10
    • 2018-04-16
    • 2011-12-14
    • 1970-01-01
    相关资源
    最近更新 更多