Enter 按键创建额外的“1”
Enter Key press creating extra "1"
我目前正在尝试编写一个简单的 Winforms 计算器,并且正在努力将 NumPad 键分配给表单按钮。到目前为止,我分配的每个按钮都可以正常工作,但 Enter 按钮除外。无论输入什么表达式,当按下物理回车键时,它都会在答案 here 的末尾添加一个单独的“1”。当仅按下表单按钮时,它可以正常工作。
有谁知道这是为什么,或者可以帮助我一些吗?
我也意识到我的一些代码不需要,所以让我放轻松,我只是个学生!
这里是关键检测代码及其引入的相关方法:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
enterButton.PerformClick();
}
if (keyData == Keys.NumPad0)
{
button10.PerformClick();
}
if (keyData == Keys.NumPad1)
{
button1.PerformClick();
}
if (keyData == Keys.NumPad2)
{
button2.PerformClick();
}
if (keyData == Keys.NumPad3)
{
button3.PerformClick();
}
if (keyData == Keys.NumPad4)
{
button4.PerformClick();
}
if (keyData == Keys.NumPad5)
{
button5.PerformClick();
}
if (keyData == Keys.NumPad6)
{
button6.PerformClick();
}
if (keyData == Keys.NumPad7)
{
button7.PerformClick();
}
if (keyData == Keys.NumPad8)
{
button8.PerformClick();
}
if (keyData == Keys.NumPad9)
{
button9.PerformClick();
}
if (keyData == Keys.Add)
{
addButton.PerformClick();
}
if (keyData == Keys.Subtract)
{
minusButton.PerformClick();
}
if (keyData == Keys.Multiply)
{
timesButton.PerformClick();
}
if (keyData == Keys.Divide)
{
divideButton.PerformClick();
}
}
private void enterButton_Click(object sender, EventArgs e)
{
operIsDone = true; //triggers final calculation
MainCalc();
}
private void MainCalc()
{
do
{
if (operation == '+')
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache += operand;
operand = 0;
break;
}
if (operation == '-')
{
if (minusButton.Tag.Equals("1"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache += operand;
minusButton.Tag = "2";
break;
}
else if (minusButton.Tag.Equals("2"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache -= operand;
break;
}
}
if(operation == '*')
{
if (timesButton.Tag.Equals("1"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache += operand;
timesButton.Tag = "2";
break;
}
else if (timesButton.Tag.Equals("2"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache *= operand;
break;
}
}
if(operation == '/')
{
if (divideButton.Tag.Equals("1"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache += operand;
divideButton.Tag = "2";
break;
}
else if (divideButton.Tag.Equals("2"))
{
operand = stringToInt(inputString);
inputString = cleared;
if (operand != 0)
{
ansCache /= operand;
}
else
{
statusLabel.Text = "Cannot Divide By Zero!";
}
break;
}
}
else if(operIsDone) { break; }
}
while (calc);
if (operIsDone)
{
statusLabel.Text = Convert.ToString(ansCache) + "";
statusText = statusLabel.Text;
}
我猜你的“1”按钮有焦点。计算完成后,回车键将被视为对 1 的点击。将回车键解释为点击具有焦点的按钮是标准行为。尝试将“1”按钮的标签索引设置为 0。它应该开始在答案的末尾添加“2”。
如果是这样的话,我会做的是在 ProcessCmdKey 中,我唯一会为回车键做的是将焦点转移到计算器的 "enter" 按钮。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
enterButton.Focus();
}
键盘回车键的常规行为将负责点击该键,因此您不必执行 PerformClick。
并且,将您的输入按钮设置为表单上的 "Accept" 按钮。这意味着只要用户在键盘上按下 "Enter",您的 "Enter" 按钮的点击事件就会被触发。基本上,按下用户物理键盘上的 "Enter" 按钮就像单击计算器的 "Enter" 按钮一样。
基本上,我想,你的问题是你按下 Enter 按钮(在计算答案之后)也被视为按下任何具有焦点的按钮,这导致“1”按钮的点击事件处理程序也被执行,它会调用 button1.PerformClick(),并且“1”会添加到计算器 "display screen".
中的内容中
就像 Agapwlesu 提到的那样,这可能是因为您的 1 号按钮被聚焦并且 enter 按下 1 按钮以及执行您想要的功能。然而,我的解决方案有点不同。
在每个按钮的属性下,您应该找到一个标记为 Tab Stop
的按钮。如果您将每个按钮设置为等于 false
,它们将不再能够聚焦,因此应防止它们在您按下回车键时被按下。
我目前正在尝试编写一个简单的 Winforms 计算器,并且正在努力将 NumPad 键分配给表单按钮。到目前为止,我分配的每个按钮都可以正常工作,但 Enter 按钮除外。无论输入什么表达式,当按下物理回车键时,它都会在答案 here 的末尾添加一个单独的“1”。当仅按下表单按钮时,它可以正常工作。 有谁知道这是为什么,或者可以帮助我一些吗? 我也意识到我的一些代码不需要,所以让我放轻松,我只是个学生!
这里是关键检测代码及其引入的相关方法:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
enterButton.PerformClick();
}
if (keyData == Keys.NumPad0)
{
button10.PerformClick();
}
if (keyData == Keys.NumPad1)
{
button1.PerformClick();
}
if (keyData == Keys.NumPad2)
{
button2.PerformClick();
}
if (keyData == Keys.NumPad3)
{
button3.PerformClick();
}
if (keyData == Keys.NumPad4)
{
button4.PerformClick();
}
if (keyData == Keys.NumPad5)
{
button5.PerformClick();
}
if (keyData == Keys.NumPad6)
{
button6.PerformClick();
}
if (keyData == Keys.NumPad7)
{
button7.PerformClick();
}
if (keyData == Keys.NumPad8)
{
button8.PerformClick();
}
if (keyData == Keys.NumPad9)
{
button9.PerformClick();
}
if (keyData == Keys.Add)
{
addButton.PerformClick();
}
if (keyData == Keys.Subtract)
{
minusButton.PerformClick();
}
if (keyData == Keys.Multiply)
{
timesButton.PerformClick();
}
if (keyData == Keys.Divide)
{
divideButton.PerformClick();
}
}
private void enterButton_Click(object sender, EventArgs e)
{
operIsDone = true; //triggers final calculation
MainCalc();
}
private void MainCalc()
{
do
{
if (operation == '+')
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache += operand;
operand = 0;
break;
}
if (operation == '-')
{
if (minusButton.Tag.Equals("1"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache += operand;
minusButton.Tag = "2";
break;
}
else if (minusButton.Tag.Equals("2"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache -= operand;
break;
}
}
if(operation == '*')
{
if (timesButton.Tag.Equals("1"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache += operand;
timesButton.Tag = "2";
break;
}
else if (timesButton.Tag.Equals("2"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache *= operand;
break;
}
}
if(operation == '/')
{
if (divideButton.Tag.Equals("1"))
{
operand = stringToInt(inputString);
inputString = cleared;
ansCache += operand;
divideButton.Tag = "2";
break;
}
else if (divideButton.Tag.Equals("2"))
{
operand = stringToInt(inputString);
inputString = cleared;
if (operand != 0)
{
ansCache /= operand;
}
else
{
statusLabel.Text = "Cannot Divide By Zero!";
}
break;
}
}
else if(operIsDone) { break; }
}
while (calc);
if (operIsDone)
{
statusLabel.Text = Convert.ToString(ansCache) + "";
statusText = statusLabel.Text;
}
我猜你的“1”按钮有焦点。计算完成后,回车键将被视为对 1 的点击。将回车键解释为点击具有焦点的按钮是标准行为。尝试将“1”按钮的标签索引设置为 0。它应该开始在答案的末尾添加“2”。
如果是这样的话,我会做的是在 ProcessCmdKey 中,我唯一会为回车键做的是将焦点转移到计算器的 "enter" 按钮。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
enterButton.Focus();
}
键盘回车键的常规行为将负责点击该键,因此您不必执行 PerformClick。
并且,将您的输入按钮设置为表单上的 "Accept" 按钮。这意味着只要用户在键盘上按下 "Enter",您的 "Enter" 按钮的点击事件就会被触发。基本上,按下用户物理键盘上的 "Enter" 按钮就像单击计算器的 "Enter" 按钮一样。
基本上,我想,你的问题是你按下 Enter 按钮(在计算答案之后)也被视为按下任何具有焦点的按钮,这导致“1”按钮的点击事件处理程序也被执行,它会调用 button1.PerformClick(),并且“1”会添加到计算器 "display screen".
中的内容中就像 Agapwlesu 提到的那样,这可能是因为您的 1 号按钮被聚焦并且 enter 按下 1 按钮以及执行您想要的功能。然而,我的解决方案有点不同。
在每个按钮的属性下,您应该找到一个标记为 Tab Stop
的按钮。如果您将每个按钮设置为等于 false
,它们将不再能够聚焦,因此应防止它们在您按下回车键时被按下。