学习了winform后,对winform有了一些初步的认识,winform通过控件来进行设计,有着所见即所得的好处,根据控件的不同属性,我们可以改变例如控件标题,颜色等等属性,根据控件所自带的事件,可以在不同情况下,执行不同的方法。
根据所学的知识,编写了一个简易的计算器,界面和代码如下。
代码:
public partial class Form1 : Form { int a; int b; int sum; bool add=false; bool jian=false; bool cheng=false; bool chu=false;//初始化变量 public Form1() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e)//根据用户不同的按键,确定用户输入的是几 { string i = textBox1.Text; textBox1.Text = i + "1"; } private void button2_Click(object sender, EventArgs e) { string i = textBox1.Text; textBox1.Text = i+"2"; } private void button3_Click(object sender, EventArgs e) { string i = textBox1.Text; textBox1.Text = i + "3"; } private void button4_Click(object sender, EventArgs e) { string i = textBox1.Text; textBox1.Text = i + "4"; } private void button5_Click(object sender, EventArgs e) { string i = textBox1.Text; textBox1.Text = i + "5"; } private void button6_Click(object sender, EventArgs e) { string i = textBox1.Text; textBox1.Text = i + "6"; } private void button7_Click(object sender, EventArgs e) { string i = textBox1.Text; textBox1.Text = i + "7"; } private void button8_Click(object sender, EventArgs e) { string i = textBox1.Text; textBox1.Text = i + "8"; } private void button9_Click(object sender, EventArgs e) { string i = textBox1.Text; textBox1.Text = i + "9"; } private void button10_Click(object sender, EventArgs e) { textBox1.Text = null;//清空计算C a = 0; b = 0; sum = 0; } private void button11_Click(object sender, EventArgs e) { try { a = Convert.ToInt32(textBox1.Text);//当用户按键为+的时候 textBox1.Text = null; add = true; } catch { MessageBox.Show("超出计算范围,请重新输入");//判断用户输入是否超出int范围 a = 0; b = 0; sum = 0; textBox1.Text = null; } } private void button15_Click(object sender, EventArgs e) { if (add == true) { try { b = Convert.ToInt32(textBox1.Text); sum = a + b; textBox1.Text = Convert.ToString(sum); add = false; } catch { MessageBox.Show("超出计算范围,请重新输入"); a = 0; b = 0; sum = 0; textBox1.Text = null; } } else if (jian == true) { try { b = Convert.ToInt32(textBox1.Text); sum = a - b; textBox1.Text = Convert.ToString(sum); jian = false; } catch { MessageBox.Show("超出计算范围,请重新输入"); a = 0; b = 0; sum = 0; textBox1.Text = null; } } else if (cheng == true) { try { b = Convert.ToInt32(textBox1.Text); sum = a * b; textBox1.Text = Convert.ToString(sum); cheng = false; } catch { MessageBox.Show("超出计算范围,请重新输入"); a = 0; b = 0; sum = 0; textBox1.Text = null; } } else if (chu == true) { try { b = Convert.ToInt32(textBox1.Text); sum = a / b; textBox1.Text = Convert.ToString(sum); chu = false; } catch { MessageBox.Show("超出计算范围,请重新输入"); a = 0; b = 0; sum = 0; textBox1.Text = null; } } else { MessageBox.Show("未知错误"); a = 0; b = 0; sum = 0; textBox1.Text = null; } } private void button12_Click(object sender, EventArgs e) { a = Convert.ToInt32(textBox1.Text);//判断用户想做什么运算 textBox1.Text = null; jian = true; } private void button13_Click(object sender, EventArgs e) { a = Convert.ToInt32(textBox1.Text);//判断用户想做什么运算 textBox1.Text = null; cheng = true; } private void button14_Click(object sender, EventArgs e) { a = Convert.ToInt32(textBox1.Text);//判断用户想做什么运算 textBox1.Text = null; chu = true; } private void button16_Click(object sender, EventArgs e) { if (textBox1.Text == null)//判断如果输入框是空的,那么就把i赋给文本框,反之 文本框的文本加上0 { textBox1.Text = "0"; } else{ string i = textBox1.Text; textBox1.Text = i + "0";} } private void button17_Click(object sender, EventArgs e) { this.Close(); } private void button18_Click(object sender, EventArgs e) { Form2 ss = new Form2(); ss.ShowDialog(); } private void Form1_Load(object sender, EventArgs e) { } }