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

 更新时间:2022年01月28日 15:01:47   作者:Dust_SongYunfei  
这篇文章主要为大家详细介绍了C#实现winform版的计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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 = "";
        }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 基于Unity3D实现3D迷宫小游戏的示例代码

    基于Unity3D实现3D迷宫小游戏的示例代码

    迷宫游戏作为经典的小游戏,一直深受大家的喜爱。本文小编将为大家详细介绍一下如何用Unity实现一个3D版的迷宫小游戏,感兴趣的可以动手试一试
    2022-03-03
  • Unity制作小地图和方向导航

    Unity制作小地图和方向导航

    这篇文章主要为大家详细介绍了Unity制作小地图和方向导航的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C#中ListView用法实例

    C#中ListView用法实例

    我们经常会在应用程序中使用列表的形式来展现一些内容,所以学好ListView是非常必需的,下面这篇文章主要给大家介绍了关于C#中ListView用法的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • Winform实现抓取web页面内容的方法

    Winform实现抓取web页面内容的方法

    这篇文章主要介绍了Winform实现抓取web页面内容的方法,代码只有短短几行,但是功能很实用,需要的朋友可以参考下
    2014-09-09
  • c# 使用handle.exe解决程序更新文件被占用的问题

    c# 使用handle.exe解决程序更新文件被占用的问题

    这篇文章主要介绍了c# 使用handle.exe解决程序更新文件被占用的问题,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C#中4种深拷贝方法介绍

    C#中4种深拷贝方法介绍

    这篇文章主要介绍了C#中4种深拷贝方法介绍,本文讲解了利用反射实现、利用xml序列化和反序列化实现、利用二进制序列化和反序列化实现、利用silverlight DataContractSerializer实现,用于在silverlight 客户端使用等4种方法,需要的朋友可以参考下
    2015-06-06
  • 为何Linq的Distinct实在是不给力

    为何Linq的Distinct实在是不给力

    本篇文章对Linq的Distinct进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C#获得MAC地址(网卡序列号)的实现代码

    C#获得MAC地址(网卡序列号)的实现代码

    这篇文章主要介绍了C#获得MAC地址的实现代码,需要的朋友可以参考下
    2014-02-02
  • 使用windows控制台调试服务的方法

    使用windows控制台调试服务的方法

    这篇文章主要介绍了使用windows控制台调试服务的方法,需要的朋友可以参考下
    2014-02-02
  • WinForm实现按名称递归查找控件的方法

    WinForm实现按名称递归查找控件的方法

    这篇文章主要介绍了WinForm实现按名称递归查找控件的方法,需要的朋友可以参考下
    2014-08-08

最新评论