VS不用人教自己看自己学两小时做的小计算器

时间:2013-06-15 07:48:26
【文件属性】:

文件名称:VS不用人教自己看自己学两小时做的小计算器

文件大小:16KB

文件格式:EXE

更新时间:2013-06-15 07:48:26

.net C# 计算器 初学 VS

什么好玩做什么 namespace WindowsFormsApplication1 { public partial class Form1 : Form { Double first = 0; String second = null; int code = 0; String result = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button(String sender) { if (result != null) { textBox1.Clear(); } if (code != 0 && second == null) { textBox1.Clear(); } String b = sender; if (textBox1.Text == null || textBox1.Text == "") { textBox1.Text = b; } else { textBox1.Text = textBox1.Text + b; } if (code != 0) { second = textBox1.Text; } result = null; label1.Focus(); } private void button1_Click(object sender, EventArgs e) { button("1"); } private void button2_Click(object sender, EventArgs e) { button("2"); } private void button3_Click(object sender, EventArgs e) { button("3"); } private void button4_Click(object sender, EventArgs e) { button("4"); } private void button5_Click(object sender, EventArgs e) { button("5"); } private void button6_Click(object sender, EventArgs e) { button("6"); } private void button7_Click(object sender, EventArgs e) { button("7"); } private void button8_Click(object sender, EventArgs e) { button("8"); } private void button9_Click(object sender, EventArgs e) { button("9"); } private void button10_Click(object sender, EventArgs e) { if (textBox1.Text == null || textBox1.Text == "") { button("0"); }else if (Convert.ToDouble(textBox1.Text) != 0) { button("0"); }else if (textBox1.Text.IndexOf(".") == 1) { button("0"); } label1.Focus(); } private void button11_Click(object sender, EventArgs e) { if (second == null) { MessageBox.Show("哥们你要算什么啊?麻烦再输入数!"); } else if (Convert.ToDouble(second) == 0 && first != 0 && code == 4) { textBox1.Text = "除数不能为零!"; } else { switch (code) { case 1: textBox1.Text = Convert.ToString(first + Convert.ToDouble(textBox1.Text)); break; case 2: textBox1.Text = Convert.ToString(first - Convert.ToDouble(textBox1.Text)); break; case 3: textBox1.Text = Convert.ToString(first * Convert.ToDouble(textBox1.Text)); break; case 4: textBox1.Text = Convert.ToString(first / Convert.ToDouble(textBox1.Text)); break; } } first = 0; second = null; code = 0; result = textBox1.Text; label1.Focus(); } private void button12_Click(object sender, EventArgs e) { if (Convert.ToDouble(textBox1.Text) != 0 || textBox1.Text == null || textBox1.Text == "") { button("00"); } else if (textBox1.Text.IndexOf(".") == 1) { button("00"); } label1.Focus(); } private void button17_Click(object sender, EventArgs e) { if (textBox1.Text.IndexOf(".") < 1) { button("."); } label1.Focus(); } private void button_2() { if (textBox1.Text == null || textBox1.Text == "") { first = 0; } else if (first == 0) { first = Convert.ToDouble(textBox1.Text); } } private void button18_Click(object sender, EventArgs e) { button_2(); code = 4; label1.Focus(); } private void button13_Click(object sender, EventArgs e) { button_2(); code = 2; label1.Focus(); } private void button14_Click(object sender, EventArgs e) { button_2(); code = 3; label1.Focus(); } private void button15_Click(object sender, EventArgs e) { button_2(); code = 1; label1.Focus(); } private void button16_Click(object sender, EventArgs e) { textBox1.Clear(); label1.Focus(); } private void Form1_KeyUp(object sender, KeyEventArgs e) { //MessageBox.Show(Convert.ToString(e.KeyCode)); if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Oemplus) { button11_Click(null, null); } else if (e.KeyCode == Keys.Divide || e.KeyCode == Keys.OemQuestion) { button18_Click(null, null); } else if (e.KeyCode == Keys.Add || e.KeyCode == Keys.Oemplus) { button15_Click(null, null); } else if (e.KeyCode == Keys.Multiply) { button14_Click(null, null); } else if (e.KeyCode == Keys.Subtract || e.KeyCode == Keys.OemMinus) { button13_Click(null, null); } else if (e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Delete) { button17_Click(null, null); } else if (e.KeyCode == Keys.Oemtilde) { button12_Click(null, null); } else if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1) { button1_Click(null, null); } else if (e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2) { button2_Click(null, null); } else if (e.KeyCode == Keys.D3 || e.KeyCode == Keys.NumPad3) { button3_Click(null, null); } else if (e.KeyCode == Keys.D4 || e.KeyCode == Keys.NumPad4) { button4_Click(null, null); } else if (e.KeyCode == Keys.D5 || e.KeyCode == Keys.NumPad5) { button5_Click(null, null); } else if (e.KeyCode == Keys.D6 || e.KeyCode == Keys.NumPad6) { button6_Click(null, null); } else if (e.KeyCode == Keys.D7 || e.KeyCode == Keys.NumPad7) { button7_Click(null, null); } else if (e.KeyCode == Keys.D8 || e.KeyCode == Keys.NumPad8) { button8_Click(null, null); } else if (e.KeyCode == Keys.D9 || e.KeyCode == Keys.NumPad9) { button9_Click(null, null); } else if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0) { button10_Click(null, null); } } private void textBox1_Enter(object sender, EventArgs e) { label1.Focus(); } } }


网友评论