C#實現(xiàn)計算器功能(winform版)
更新時間:2022年01月28日 15:01:47 作者:Dust_SongYunfei
這篇文章主要為大家詳細介紹了C#實現(xiàn)winform版的計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#實現(xiàn)計算器功能的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
Random rad = new Random(); // 實例化隨機對象 ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2; ? ? ? ? ? ? this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2; ? ? ? ? ? ? this.Text = "計算器"; ? ? ? ? ? ? textBox1.ReadOnly =true;// 文本框無法輸入字符 ? ? ? ? ? ? foreach (Control ctl in this.Controls) ? ? ? ? ? ? { ? // 獲取所有按鈕 ?改變背景顏色和數(shù)字和符號顏色 ? ? ? ? ? ? ? ? if (ctl is Button) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Button btn = ctl as Button; ? ? ? ? ? ? ? ? ? ? btn.BackColor = Color.FromArgb(rad.Next(256),rad.Next(256),rad.Next(256),rad.Next(256)); ? ? ? ? ? ? ? ? ? ? btn.ForeColor = Color.FromArgb(rad.Next(256), rad.Next(256), rad.Next(256)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void 關(guān)閉ToolStripMenuItem_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? this.Close();// 關(guān)閉窗體 ? ? ? ? } ? ? ? ? char fuhao;// 接收符號 ? ? ? ? double temp, num; // temp第一個值,num為第二個值 ? ? ? ?// 1 ? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "1"; ? ? ? ? } ? ? ? ? // 2 ? ? ? ? private void button2_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "2"; ? ? ? ? } ? ? ? ? // 3 ? ? ? ? private void button3_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "3"; ? ? ? ? } ? ? ? ? // 4 ? ? ? ? private void button5_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "4"; ? ? ? ? } ? ? ? ? // 5 ? ? ? ? private void button6_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "5"; ? ? ? ? } ? ? ? ? // 6 ? ? ? ? private void button7_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "6"; ? ? ? ? } ? ? ? ? // 7 ? ? ? ? private void button9_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "7"; ? ? ? ? } ? ? ? ? // 8 ? ? ? ? private void button10_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "8"; ? ? ? ? } ? ? ? ? // 9 ? ? ? ? private void button11_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "9"; ? ? ? ? } ? ? ? ? // 0 ? ? ? ? private void button15_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "0"; ? ? ? ? } ? ? ? ? //點 ? ? ? ? private void button16_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "."; ? ? ? ? } ? ? ? ? // + ? ? ? ? private void button4_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '+'; ? ? ? ? ? ? textBox1.Text += "+"; ? ? ? ? ? ? //textBox1.Text = null; ? ? ? ? } ? ? ? ? // - ? ? ? ? private void button8_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '-'; ? ? ? ? ? ? textBox1.Text = null; ? ? ? ? } ? ? ? ? // × ? ? ? ? private void button12_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '×'; ? ? ? ? ? ? textBox1.Text = null; ? ? ? ? } ? ? ? ? // ÷ ? ? ? ? private void button20_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '÷'; ? ? ? ? ? ? textBox1.Text = null; ? ? ? ? } ? ? ? ? // % ? ? ? ? private void button19_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '%'; ? ? ? ? ? ? textBox1.Text = null; ? ? ? ? } ? ? ? ? // = ? ? ? ? private void button13_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? temp = double.Parse(textBox1.Text); ? ? ? ? ? ? switch (fuhao) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? case '+': ? ? ? ? ? ? ? ? ? ? num += temp; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '-': ? ? ? ? ? ? ? ? ? ? num -= temp; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '×': ? ? ? ? ? ? ? ? ? ? num *= temp; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '÷': ? ? ? ? ? ? ? ? ? ? num /= temp; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '%': ? ? ? ? ? ? ? ? ? ? num %= temp; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text = num.ToString(); ? ? ? ? ? ? fuhao = ' '; ? ? ? ? ? ? num = 0; ? ? ? ? ? ? temp = 0; ? ? ? ? } ? ? ? ? // 刪除 ? ? ? ? private void button18_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text.Length > 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //清空 ? ? ? ? private void button17_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章

Winform實現(xiàn)抓取web頁面內(nèi)容的方法
這篇文章主要介紹了Winform實現(xiàn)抓取web頁面內(nèi)容的方法,代碼只有短短幾行,但是功能很實用,需要的朋友可以參考下
2014-09-09 
C#獲得MAC地址(網(wǎng)卡序列號)的實現(xiàn)代碼
這篇文章主要介紹了C#獲得MAC地址的實現(xiàn)代碼,需要的朋友可以參考下
2014-02-02