前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >五分钟自制计算器

五分钟自制计算器

作者头像
跋扈洋
发布2021-04-08 22:02:20
1K0
发布2021-04-08 22:02:20
举报
文章被收录于专栏:物联网知识物联网知识

需求

我们在生活中,或多或少都使用过计算器。那么我们是否可以自己制作一款计算器呢,答案当然是可以的。这里我介绍一款通过C#编写的计算机,大家可以用来借鉴。

功能介绍

我们需要将0~9这10个数字的按键,还需要四则运算需要的加、减、乘、除等,具体界面如下。

步骤

1. 打开VS,创建Windows窗体应用

2. 选择项目文件夹

根据自己的实际存储位置,进行更改。

3. 打开 视图 中的工具箱,通过拖拉相关配件,进行计算器页面的设计,注意相关按钮的名字需要自己编辑。

4. 双击相关配件,就可进行代码编辑页面。

我这里将程序直接给大家,大家注意我这里的组件和你自己的可能不相同,大家根据自己的组件名字进行更改。

代码语言:javascript
复制
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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        double a = 0;
        double b = 0;   
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
          
            b = double.Parse(textBox1.Text);
            textBox1.Text += "+";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text += "1";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text += "2";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text += ".";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            var res = dt.Compute(textBox1.Text, "");
            textBox1.Text = Convert.ToString(res);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            b = double.Parse(textBox1.Text);
            textBox1.Text += "-";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            b = double.Parse(textBox1.Text);
            textBox1.Text += "*";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            b = double.Parse(textBox1.Text);
            textBox1.Text += "/";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text += "3";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text += "4";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox1.Text += "5";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text += "6";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox1.Text += "7";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox1.Text += "8";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            textBox1.Text += "9";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            textBox1.Text += "0";
        }
    }
}

实现页面

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-03-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 物联网知识 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 需求
  • 功能介绍
  • 步骤
  • 实现页面
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档