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为第二个值
       // 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 = "";
        }
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值