c#如何获得keydown和按钮点击做同样的事情?

时间:2010-06-07 06:19:15

标签: c# calculator

我是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 个答案:

答案 0 :(得分:1)

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

if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.NumPad1)
{
   // Do what you want to do.
}

答案 1 :(得分: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... 
}

通过这种方式,在numPad ...

上键入时,您还可以获得一个很好的按键效果

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

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();
    }
}