c#.net之窗体应用程序按钮版计算器

c#.net之窗体应用程序按钮版计算器_第1张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 按钮计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /*1.显示屏
         * 2.0-9数字按钮和+-×÷=按钮计算器
         * 3.实现输入第一个数字
         * 4.输入运算符号
         * 5.输入第二个数字
         * 6.点击=计算出结果,结果显示在显示屏上
        */
        //随机对象实例化
        Random si = new Random();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
            this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
            textBox1.BackColor = Color.Pink;
            textBox1.ForeColor = Color.White;
            textBox1.Font = new Font("",30);
            textBox1.ReadOnly = true;
            textBox2.Font = new Font("", 30);
            textBox2.ReadOnly = true;
            //foreach循环找到所有的button按钮
            foreach (Control ctl in this.Controls)
            {
                if (ctl is Button)
                {
                    Button btn = (Button)ctl;
                    btn.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
                    btn.ForeColor = Color.White;
                    btn.Font = new Font("", 20);
                    //设置平面显示
                    btn.FlatStyle = FlatStyle.Flat;
                    //去掉按钮的边框
                    btn.FlatAppearance.BorderSize = 0;

                }
            }
        }
        //定义一个数字接收第一次输入的数字
        double num1;
        //定义一个值来确定上次按得是否是符号
        bool isCala = false;
        //定义一个值来确定是否输入了点符号
        bool isdian = false;
        //定义一个值来接收运算符号
        string type;
        //定义一个值来确定文本框2是否按下等号
        bool dy = false;
        //1
        private void button1_Click(object sender, EventArgs e)
        {
            button1.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy==true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy= false;
            }
            if (isCala==true)
            {
                isCala = false;
                textBox1.Text = "";
            }
                textBox1.Text += 1;
                textBox2.Text += 1;
        }
        //2
        private void button2_Click(object sender, EventArgs e)
        {
            button2.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isCala == true)
            {
                isCala = false;
                textBox1.Text = "";
            }
            textBox1.Text += 2;
            textBox2.Text += 2;
        }
        //3
        private void button3_Click(object sender, EventArgs e)
        {
            button3.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isCala == true)
            {
                isCala = false;
                textBox1.Text = "";
            }
            textBox1.Text += 3;
            textBox2.Text += 3;
        }
        //4
        private void button4_Click(object sender, EventArgs e)
        {
            button4.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isCala == true)
            {
                isCala = false;
                textBox1.Text = "";
            }
            textBox1.Text += 4;
            textBox2.Text += 4;
        }
        //5
        private void button5_Click(object sender, EventArgs e)
        {
            button5.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isCala == true)
            {
                isCala = false;
                textBox1.Text = "";
            }
            textBox1.Text += 5;
            textBox2.Text += 5;
        }
        //6
        private void button6_Click(object sender, EventArgs e)
        {
            button6.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isCala == true)
            {
                isCala = false;
                textBox1.Text = "";
            }
            textBox1.Text += 6;
            textBox2.Text += 6;
        }
        //7
        private void button7_Click(object sender, EventArgs e)
        {
            button7.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isCala == true)
            {
                isCala = false;
                textBox1.Text = "";
            }
            textBox1.Text += 7;
            textBox2.Text += 7;
        }
        //8
        private void button8_Click(object sender, EventArgs e)
        {
            button8.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isCala == true)
            {
                isCala = false;
                textBox1.Text = "";
            }
            textBox1.Text += 8;
            textBox2.Text += 8;
        }
        //9
        private void button9_Click(object sender, EventArgs e)
        {
            button9.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isCala == true)
            {
                isCala = false;
                textBox1.Text = "";
            }
            textBox1.Text += 9;
            textBox2.Text += 9;
        }
        //0
        private void button10_Click(object sender, EventArgs e)
        {
            button10.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isCala == true)
            {
                isCala = false;
                textBox1.Text = "";
            }
            textBox1.Text += 0;
            textBox2.Text += 0;
        }
        //.
        private void button16_Click(object sender, EventArgs e)
        {
            button16.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            if (dy == true)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                dy = false;
            }
            if (isdian==false)
            {
                textBox1.Text += ".";
                textBox2.Text += ".";
                isdian = true;
            }
        }
        //+
        private void button11_Click(object sender, EventArgs e)
        {
            type = "+";
            if (!double.TryParse(textBox1.Text,out num1))
            {
                return;
            }
            else
            {
                num1 = double.Parse(textBox1.Text);
            }
            isCala = true;
            isdian = false;
            textBox2.Text += "+";
            button11.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
        }
        //-
        private void button12_Click(object sender, EventArgs e)
        {
            type = "-";
            if (!double.TryParse(textBox1.Text, out num1))
            {
                return;
            }
            else
            {
                num1 = double.Parse(textBox1.Text);
            }
            isCala = true;
            isdian = false;
            textBox2.Text += "-";
            button12.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
        }


        //*
        private void button13_Click(object sender, EventArgs e)
        {
            type = "*";
            if (!double.TryParse(textBox1.Text, out num1))
            {
                return;
            }
            else
            {
                num1 = double.Parse(textBox1.Text);
            }
            isCala = true;
            isdian = false;
            textBox2.Text += "*";
            button13.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
        }


        //÷
        private void button14_Click(object sender, EventArgs e)
        {
            type = "/";
            if (!double.TryParse(textBox1.Text, out num1))
            {
                return;
            }
            else
            {
                num1 = double.Parse(textBox1.Text);
            }
            isCala = true;
            isdian = false;
            textBox2.Text += "/";
            button14.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
        }


        //=
        double num2;
        private void button15_Click(object sender, EventArgs e)
        {
            if (!double.TryParse(textBox1.Text,out num2))
            {
                return;
            }
            else
            {                   
             num2 = double.Parse(textBox1.Text);
            }
            button15.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
            isCala = true;
            dy = true;
            if (type=="+")
            {
                textBox1.Text = (num1 + num2).ToString();
            }
            if (type == "-")
            {
                textBox1.Text = (num1 - num2).ToString();
            }
            if (type == "*")
            {
                textBox1.Text = (num1 * num2).ToString();
            }
            if (type == "/")
            {
                textBox1.Text = (num1 / num2).ToString();
            }
            textBox2.Text +="="+ textBox1.Text;
        }

        //清除
        private void button17_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            button17.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
        }
        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length>0)
            {
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            }
            if (textBox2.Text.Length > 0)
            {
                textBox2.Text = textBox1.Text.Substring(0, textBox2.Text.Length - 1);
            }
        }
    }
}

你可能感兴趣的:(c#.net之窗体应用程序按钮版计算器)