2

我是 C# 的新手。我正在学习制作一个计算器,就像 ms windows 计算器一样。当我点击它时,按钮可以工作,但我希望小键盘也可以工作。假设用户键入'0',它应该与他单击我的 gui 上的 0 按钮相同。这是我的 0 按钮单击事件。

private void button0_Click(object sender, EventArgs e)
        {
            checkifequa();
            textBox1.Text = textBox1.Text + "0";
        }

我如何让keydown工作?

编辑

这是完整的来源。这是youtube上的某个人写的,我只是在修改和尝试学习。请指出任何错误并提出更好的方法。


using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

namespace WindowsFormsApplication1 { public partial class Form1 : Form { bool plus = false; bool minus = false; bool into = false; bool divd = false; bool equa = false; public Form1() { InitializeComponent(); }

private void button0_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "0"; } private void button1_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "1"; } private void checkifequa() { if (equa) textBox1.Text = ""; equa = false; } private void button2_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "2"; } private void button3_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "3"; } private void button4_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "4"; } private void button5_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "5"; } private void button6_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "6"; } private void button7_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "7"; } private void button8_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "8"; } private void button9_Click(object sender, EventArgs e) { checkifequa(); textBox1.Text = textBox1.Text + "9"; } private void button11_Click(object sender, EventArgs e) { checkifequa(); if(textBox1.Text.Contains(".")) { return; } else { textBox1.Text=textBox1.Text+"."; } } private void plusminus_Click(object sender, EventArgs e) { if (textBox1.Text.Contains("-")) { textBox1.Text = textBox1.Text.Remove(0, 1); } else { textBox1.Text = "-" + textBox1.Text; } } private void plus_Click(object sender, EventArgs e) { if (textBox1.Text == "") { return; } else { plus = true; textBox1.Tag = textBox1.Text; textBox1.Text = ""; } } private void equal_Click(object sender, EventArgs e) { equa = true; if (plus) { decimal dec = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text); textBox1.Text = dec.ToString(); } if (minus) { decimal dec = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text); textBox1.Text = dec.ToString(); } if (into) { decimal dec = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text); textBox1.Text = dec.ToString(); } if (divd) { decimal dec = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text); textBox1.Text = dec.ToString(); } } private void substract_Click(object sender, EventArgs e) { if (minus) if (textBox1.Text == "") { return; } else { minus = true; textBox1.Tag = textBox1.Text; textBox1.Text = ""; } } private void multiply_Click(object sender, EventArgs e) { if (textBox1.Text == "") { return; } else { into = true; textBox1.Tag = textBox1.Text; textBox1.Text = ""; } } private void divide_Click(object sender, EventArgs e) { if (textBox1.Text == "") { return; } else { divd = true; textBox1.Tag = textBox1.Text; textBox1.Text = ""; } } private void clear_Click(object sender, EventArgs e) { plus = minus = into = divd = equa = false; textBox1.Text = ""; textBox1.Tag = ""; } void Form1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar >= 48 && e.KeyChar <= 57) { switch (e.KeyChar) { case (char)48: button0.PerformClick(); break; case (char)49: button1.PerformClick(); break; case (char)50: button2.PerformClick(); break; case (char)51: button3.PerformClick(); break; case (char)52: button4.PerformClick(); break; case (char)53: button5.PerformClick(); break; case (char)54: button6.PerformClick(); break; case (char)55: button7.PerformClick(); break; case (char)56: button8.PerformClick(); break; case (char)57: button9.PerformClick(); break; } } } }

}

4

4 回答 4

1

检查窗口的 PreviewKeyDown 事件并做一些这样的事情

if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.NumPad1)
{
   // Do what you want to do.
}
于 2010-06-07T06:32:29.057 回答
1

您可以为具有相同代码/功能的所有按钮添加相同的事件处理程序,例如:

buttonZero.Click += numberButton_Click;
buttonOne.Click += numberButton_Click;
...

buttonPlus.Click += numberButton_Click;
buttonMinus.Click += numberButton_Click;
...

private void numberButton_Click(object sender, EventArgs e)
{
   checkifequa();
   var numButton = sender as Button;
   if(numButton != null)
      textBox1.Text = textBox1.Text + numButton.Text; // supposing your num buttons have only the number as text (otherwise you could use the Tag property of buttons)
}

private void operatorButton_Click(object sender, EventArgs e)
{
   checkifequa();
   var operatorButton = sender as Button;
   if(operatorButton != null)
      textBox1.Text = textBox1.Text + operatorButton .Text; // supposing your operator buttons have only the operator as text (otherwise you could use the Tag property of button)
}
// ...

对于键盘事件,正如 cre-johnny07 所建议的:您可以处理 previewKeyDown 事件或 KeyDown 事件并执行以下操作:

private void parentControl_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.NumPad0)
        this.buttonZero.PerformClick();
    else if (e.KeyCode == Keys.NumPad1)
        this.buttonOne.PerformClick();
    // and so on... 
}

这样,您在小键盘上打字时也会有一个不错的按钮点击效果......

于 2010-06-07T07:18:06.820 回答
0

我会从点击/按键处理程序中取出“业务逻辑”。此外,我会将数据存储在私有属性中,而不是 TextBox 的标记中。我遗漏了您通常拥有的验证码。

private string FirstNumber { get; set; }
private string SecondNumber { get; set; }
private bool IsSecondNumberBeingEntered { get; set; }

private Operation SelectedOperation { get; set;}
private enum Operation
{
    Add,
    Subtract,
    Multiply,
    Divide
}

private void button0_Click(object sender, EventArgs e)
{
    this.AddNumber(0);
    textBox1.Text = textBox1.Text + "0";
}

// Etc.

// In PreviewKeyDown:
if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)
{
    this.AddNumber(0);
}

private void AddNumber(int number)
{
    if (IsSecondNumberBeingEntered)
    {
        SecondNumber += number.ToString();
    }
    else
    {
        FirstNumber += number.ToString();
    }
}

private decimal Calculate()
{
    switch (SelectedOperation)
    {
        case Operation.Add:
            return Convert.ToDecimal(FirstNumber) + Convert.ToDecimal(SecondNumber);
        case Operation.Subtract:
            return Convert.ToDecimal(FirstNumber) - Convert.ToDecimal(SecondNumber);
        // etc.
        default:
            throw new ArgumentOutOfRangeException();
    }
}
于 2010-06-07T08:13:57.527 回答
0

您还可以做的是使鼠标单击事件和 keydown 事件都指向同一事物。如果您进入事件下的控件属性,请为 keydown 和 mouseclick 选择相同的事件。只需在您的代码中添加一个“if”语句来检查谁在调用它。

根据我的经验,程序的结构变得更加整洁。

于 2010-06-07T07:24:51.447 回答