我是 C# 的新手。我做了一个简单的计算器。但它没有键盘输入。如何启用它?
当从键盘按下数字 5 时,我想在文本框中得到 5 。
我也想在文本字段中隐藏鼠标光标。现在,当应用程序启动时,鼠标光标会出现在文本字段中。
这是我的代码:
namespace MyCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//textBox1.Text = "0";
}
double num1=0, num2, result;
string op;
private void button_plus_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "+";
}
private void button_minus_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "-";
}
private void button_mul_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "*";
}
private void button_div_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "/";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
private void button0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
private void button00_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "00";
}
private void button_point_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ".";
}
private void button_clear_Click(object sender, EventArgs e)
{
textBox1.Text = String.Empty;
}
private void button_result_Click(object sender, EventArgs e)
{
calculate(op);
}
public void calculate( string op)
{
num2 = Convert.ToDouble(textBox1.Text);
switch(op)
{
case "+" : result=num1+num2;
textBox1.Text = result.ToString(); break;
case "-": result = num1 - num2;
textBox1.Text = result.ToString(); break;
case "*": result = num1 * num2;
textBox1.Text = result.ToString(); break;
case "/": result = num1 / num2;
textBox1.Text = result.ToString(); break;
}
num1 = 0; num2 = 0;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Font = new Font("Arial",12, FontStyle.Bold);
textBox1.Cursor = Cursors.Arrow;
}
}
}