日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現計算器功能(winform版)_C#教程

作者:Dust_SongYunfei ? 更新時間: 2022-04-10 編程語言

本文實例為大家分享了C#實現計算器功能的具體代碼,供大家參考,具體內容如下

代碼:

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)
? ? ? ? ? ? { ? // 獲取所有按鈕 ?改變背景顏色和數字和符號顏色
? ? ? ? ? ? ? ? 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 關閉ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? this.Close();// 關閉窗體
? ? ? ? }
? ? ? ? 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 = "";
? ? ? ? }

原文鏈接:https://blog.csdn.net/dust__/article/details/103076040

欄目分類
最近更新