C#实现计算器功能(winform版)

本文实例为大家分享了C#实现计算器功能的具体代码,供大家参考,具体内容如下
C#实现计算器功能(winform版)
文章图片

代码:

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为第二个值// 1private void button1_Click(object sender, EventArgs e){textBox1.Text += "1"; }// 2private void button2_Click(object sender, EventArgs e){textBox1.Text += "2"; }// 3private void button3_Click(object sender, EventArgs e){textBox1.Text += "3"; }// 4private void button5_Click(object sender, EventArgs e){textBox1.Text += "4"; }// 5private void button6_Click(object sender, EventArgs e){textBox1.Text += "5"; }// 6private void button7_Click(object sender, EventArgs e){textBox1.Text += "6"; }// 7private void button9_Click(object sender, EventArgs e){textBox1.Text += "7"; }// 8private void button10_Click(object sender, EventArgs e){textBox1.Text += "8"; }// 9private void button11_Click(object sender, EventArgs e){textBox1.Text += "9"; }// 0private 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 = ""; }

【C#实现计算器功能(winform版)】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读