利用c#語言編寫簡單計算器:
核心知識點:
MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序號
MessageBox.Show(Convert.ToString(comboBox1.SelectedItem));//下拉內容
MessageBox.Show(Convert.ToString(comboBox1.SelectedText));//數據庫會用到
MessageBox.Show(Convert.ToString(comboBox1.SelectedValue));//數據庫會用到
源碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace 簡單計算器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e)//加法 { string str1 = textBox1.Text;//str1保存第一個文本框輸入的內容 string str2 = textBox2.Text;//str2保存第二個文本框輸入的內容 int i1, i2; if (!int.TryParse(str1, out i1))//等價於 if (!int.TryParse(str1, out i1)==false),將第一個文本框內容字符串轉換成整型數據 { MessageBox.Show("第一個數不是合法的整數");//彈出消息對話框 return;//不要忘了return,僅僅退出所在的函數 } if (int.TryParse(str2, out i2) == false)//將第二個文本框內容字符串轉換成整型數據 { MessageBox.Show("第二個數字不是合法的整數");//彈出消息對話框 return; } int i3 = i1 + i2;//進行運算 textBox3.Text = Convert.ToString(i3);// 等價於textBox3 = i3.ToString(); } private void button2_Click(object sender, EventArgs e)//單擊隱藏文本框 { textBox1.Hide();//第一個文本框隱藏 textBox2.Hide();//第二個文本框隱藏 textBox3.Hide();//第三個文本框隱藏 textBox4.Hide(); textBox5.Hide(); textBox6.Hide(); textBox7.Hide(); textBox8.Hide(); textBox9.Hide(); textBox10.Hide(); textBox11.Hide(); textBox12.Hide(); } private void button3_Click(object sender, EventArgs e)//單擊顯示文本框 { textBox1.Show();//第一個文本框顯示 textBox2.Show();//第二個文本框顯示 textBox3.Show();//第三個文本框顯示 textBox4.Show(); textBox5.Show(); textBox6.Show(); textBox7.Show(); textBox8.Show(); textBox9.Show(); textBox10.Show(); textBox11.Show(); textBox12.Show(); } private void button4_Click(object sender, EventArgs e)//減法 { string str3 = textBox4.Text; string str4 = textBox5.Text; int i3, i4; if (!int.TryParse(str3,out i3)) { MessageBox.Show("第一個數不是合法的整數"); return; } if (!int.TryParse(str4,out i4)) { MessageBox.Show("第二個數不是合法的數據"); } int i5 = i3 -i4; textBox6.Text = Convert.ToString(i5); } private void button5_Click(object sender, EventArgs e)//乘法 { string str3 = textBox7.Text; string str4 = textBox8.Text; int i3, i4; if (!int.TryParse(str3, out i3)) { MessageBox.Show("第一個數不是合法的整數"); return; } if (!int.TryParse(str4, out i4)) { MessageBox.Show("第二個數不是合法的數據"); } int i5 = i3 *i4; textBox9.Text = Convert.ToString(i5); } private void button6_Click(object sender, EventArgs e)//除法 { string str3 = textBox10.Text; string str4 = textBox11.Text; int i3, i4; if (!int.TryParse(str3, out i3)) { MessageBox.Show("第一個數不是合法的整數"); return; } if (!int.TryParse(str4, out i4)) { MessageBox.Show("第二個數不是合法的數據"); } int i5 = i3 / i4; textBox12.Text = Convert.ToString(i5); } } }
程序截圖:

計算器版本2.0
源碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 計算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { /******************************** MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序號 MessageBox.Show(Convert.ToString(comboBox1.SelectedItem));//下拉內容 MessageBox.Show(Convert.ToString(comboBox1.SelectedText));//數據庫會用到 MessageBox.Show(Convert.ToString(comboBox1.SelectedValue));//數據庫會用到 **********************************/ string s1 = textBox1.Text; string s2 = textBox2.Text; int i1, i2; i1 = Convert.ToInt32(s1); i2 = Convert.ToInt32(s2); int result; switch (comboBox1.SelectedIndex) //多選框的序號 { case 0: result = i1 + i2; break; case 1: result = i1 - i2; break; case 2: result = i1 * i2; break; case 3: if (i2 == 0) //檢查除數是否為零 { MessageBox.Show("除數不能為零!!!"); return; } result = i1 / i2; break; default://防患於未然 throw new Exception("未知的運算符"); } textBox3.Text = Convert.ToString(result); } } }
運行截圖:





