这个程序按加号减号乘号除号没反应,为什么啊???
按数字键和enter就行,但是加号减号乘号除号没反应,谁帮我看一下??namespace cal
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double num1=0, num2=0, result=0;
bool point;
string op;
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "0.";
textBox1.TextAlign = HorizontalAlignment.Right;
}
public void getnumber(int num)
{
if (point)
{ textBox1.Text = textBox1.Text + num; }
else
{
if (textBox1.Text == "0.")
{ textBox1.Text = num + "."; }
else
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1) + num + ".";
}
}
}
private void button1_Click(object sender, EventArgs e)
{
getnumber(1);
}
private void button2_Click(object sender, EventArgs e)
{
getnumber(2);
}
private void button3_Click(object sender, EventArgs e)
{
getnumber(3);
}
private void button4_Click(object sender, EventArgs e)
{
getnumber(4);
}
private void button5_Click(object sender, EventArgs e)
{
getnumber(5);
}
private void button6_Click(object sender, EventArgs e)
{
getnumber(6);
}
private void button7_Click(object sender, EventArgs e)
{
getnumber(7);
}
private void button8_Click(object sender, EventArgs e)
{
getnumber(8);
}
private void button9_Click(object sender, EventArgs e)
{
getnumber(9);
}
private void button10_Click(object sender, EventArgs e)
{
getnumber(0);
}
private void buttonpoint_Click(object sender, EventArgs e)
{
point = true;
}
private void buttonadd_Click(object sender, EventArgs e)
{
op = "add";
num1 = double.Parse(textBox1.Text);
textBox1.Text = "0.";
point = false;
textBox1.Focus();
}
private void buttonsub_Click(object sender, EventArgs e)
{
op = "sub";
num1 = double.Parse(textBox1.Text);
textBox1.Text = "0.";
point = false;
}
private void buttonmul_Click(object sender, EventArgs e)
{
op = "mul";
num1 = double.Parse(textBox1.Text);
textBox1.Text = "0.";
point = false;
}
private void buttondiv_Click(object sender, EventArgs e)
{
op = "div";
num1 = double.Parse(textBox1.Text);
textBox1.Text = "0.";
point = false;
}
private void buttonclear_Click(object sender, EventArgs e)
{
num1 = 0;
num2 = 0;
textBox1.Text = "0.";
}
private void buttonresult_Click(object sender, EventArgs e)
{
num2 = double.Parse(textBox1.Text);
switch (op)
{
case "add":
result = num1 + num2;
textBox1.Text = result.ToString();
break;
case "sub":
result = num1 - num2;
textBox1.Text = result.ToString();
break;
case "mul":
result = num1 * num2;
textBox1.Text = result.ToString();
break;
case "div":
if (num2 == 0)
{
textBox1.Text = "除数不能为0!";
}
else
{
result = num1 / num2;
textBox1.Text = result.ToString();
}
break;
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
switch((Keys)e.KeyChar)
{
case Keys.D1: button1_Click(sender, e); break;
case Keys.D2: button2_Click(sender, e); break;
case Keys.D3: button3_Click(sender, e); break;
case Keys.D4: button4_Click(sender, e); break;
case Keys.D5: button5_Click(sender, e); break;
case Keys.D6: button6_Click(sender, e); break;
case Keys.D7: button7_Click(sender, e); break;
case Keys.D8: button8_Click(sender, e); break;
case Keys.D9: button9_Click(sender, e); break;
case Keys.D0: button10_Click(sender, e); break;
case Keys.Add: this.buttonadd.PerformClick(); break;
case Keys.Subtract: buttonsub_Click(sender, e); break;
case Keys.Multiply: buttonmul_Click(sender, e); break;
case Keys.Divide: buttondiv_Click(sender, e); break;
case Keys.Enter: buttonresult_Click(sender, e); break;
case Keys.C: buttonclear_Click(sender, e); break;
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
}
}
[ 本帖最后由 molong1989 于 2010-3-21 19:27 编辑 ]