winform程序之comboBox控件应用四则运算(简单)

如上图布局,

=按钮事件:

<textarea cols="50" rows="15" name="code" class="c-sharp"> public Form1() { InitializeComponent(); //设置combobox的默认运算符为+,这样就可以把switch里的default去掉了 cmbcalc.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { int i1,i2,i3=0; if (int.TryParse(txtnumber1.Text, out i1) == false) { MessageBox.Show("第一个数为非法数"); txtnumber1.Focus(); txtnumber1.BackColor = Color.Red; return; } else { txtnumber1.BackColor = Color.White; } if (int.TryParse(txtnumber2.Text, out i2) == false) { MessageBox.Show("第二个数为非法数"); txtnumber2.Focus(); txtnumber2.BackColor = Color.Red; return; } else { txtnumber2.BackColor = Color.White; } int index = cmbcalc.SelectedIndex; switch (index) { case 0: i3 = i1 + i2; break; case 1: i3 = i1 - i2; break; case 2: i3 = i1 * i2; break; case 3: i3 = i1 / i2; break; //在未选择运算符的情况下 //default: // MessageBox.Show("未选择运算符"); // cmbcalc.SelectedIndex = 0; // return; } txtnumber3.Text = i3.ToString(); } </textarea> 

你可能感兴趣的:(object,button,WinForm)