1.C#WinForm基础制作简单计算器
生活随笔
收集整理的這篇文章主要介紹了
1.C#WinForm基础制作简单计算器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
利用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);}} }運行截圖:
?
轉載于:https://www.cnblogs.com/xingyunblog/p/3888290.html
總結
以上是生活随笔為你收集整理的1.C#WinForm基础制作简单计算器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity自带网络功能——Network
- 下一篇: webServices 应该支持Get和