[英]Use Enter on keyboard as equal for calculator
當我嘗試在鍵盤上使用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;
}
}
}
這里的問題是當你按下回車鍵時,帶焦點的控件會抓住事件。 單擊某個按鈕時,會為其指定焦點,因此下次單擊“輸入”時,該按鈕將抓取該事件,如果是按鈕,則輸入鍵將被解釋為按下按鈕。
你想要的是表單始終具有焦點,這樣你就知道關鍵事件將始終轉到表單而不是任何其他控件(例如按鈕)。 為了做到這一點,請看看這個問題的答案: C#停止按鈕從獲得焦點點擊
如果您需要更多幫助,請告訴我們!
btn_equalButton.Focus();
點擊按鈕后將其放入,只需將焦點更改為等於。 當您單擊回車鍵時,它將執行它所關注的按鈕的單擊事件。
所有控件上的TabStop = false
設置tab index to -1
也可以
這樣按壓標簽不會移動焦點
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.