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

作者:Dust_SongYunfei 时间:2021-07-16 06:40:15 

本文实例为大家分享了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为第二个值
       // 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

标签:C#,计算器
0
投稿

猜你喜欢

  • 图文详解SpringBoot中Log日志的集成

    2023-05-27 09:55:13
  • 多用多学之Java中的Set,List,Map详解

    2022-10-20 12:42:36
  • java8 Stream list to Map key 重复 value合并到Collectio的操作

    2022-02-01 23:53:58
  • Java实现二叉树的深度优先遍历和广度优先遍历算法示例

    2021-07-16 12:37:17
  • MySQL+SSM+Ajax上传图片问题

    2023-01-26 04:22:52
  • Spring Cloud Eureka 服务上下线监控的实现

    2022-02-18 21:06:15
  • Spring Security实现HTTP认证

    2021-10-31 14:21:47
  • 使用C#创建Windows服务的实例代码

    2022-07-30 05:58:48
  • 实战分布式医疗挂号系统开发医院科室及排班的接口

    2021-05-27 14:21:16
  • c# 使用Task实现非阻塞式的I/O操作

    2023-07-21 23:27:39
  • Android实现多线程下载图片的方法

    2021-10-16 08:37:43
  • 浅谈Java方法调用的优先级问题

    2023-07-01 13:40:08
  • 学习Java之如何正确地向上转型与向下转型

    2022-10-12 09:58:10
  • C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)

    2022-05-17 11:46:16
  • Java JVM内存区域详解

    2021-05-25 18:44:43
  • Springboot2.x+ShardingSphere实现分库分表的示例代码

    2023-11-26 01:34:07
  • 深入浅析SpringBoot自动配置原理

    2022-11-19 01:49:24
  • Mybatis中的常用OGNL表达式

    2023-09-29 10:48:33
  • C#实现猜数字游戏

    2021-11-16 07:29:58
  • JavaWeb项目部署到服务器详细步骤详解

    2023-11-29 11:15:20
  • asp之家 软件编程 m.aspxhome.com