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

本文详细介绍了如何使用C# .NET语言开发一个简单的窗口应用程序,该程序实现基本的数学运算功能,包括加、减、乘、除。通过创建窗体、添加按钮、设置事件监听,来构建一个用户友好的桌面计算器应用。
摘要由CSDN通过智能技术生成

在这里插入图片描述

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
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值