我是 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;
}
}
}
}
}