数据库作业

数据库作业2——利用c#设计可视化简易计算器 (加减乘除)

在vs里新建项目,找到c#中的Windows窗体应用,命名即可。
如下:
在这里插入图片描述
在视图里打开工具箱:也可用快捷键(Ctrl+Alt+x)
在这里插入图片描述
注:打开后工具箱就固定在左侧,再选工具时直接点击即可.

在这里我们只需用到如下两种工具:可以拖动至设计器也可双击
在这里插入图片描述
在这里插入图片描述
构建大致界面:右键按钮选属性可以调节字体颜色及大小
在这里插入图片描述
双击按钮编码,代码如下:

using System;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class 计算器 : Form
    {
        public 计算器()
        {
            InitializeComponent();
        }
        double a, b, c;//两个预置数,a、b暂存,c作为结果输出
        string f;//一个标识符用于判断+-*/
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Text = "";

            textBox1.Text += "1";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

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

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

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

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

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

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

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

        private void button11_Click(object sender, EventArgs e)
        {
            b = Convert.ToDouble(textBox1.Text);
            if(f=="+")
            c = a + b;
            if (f == "-")
                c = a -b;
            if (f == "*")
                c = a *b;
            if (f == "/")
                c = a /b;
            textBox1.Text = c.ToString();

        }

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

        private void button12_Click(object sender, EventArgs e)
        {
            a = Convert.ToDouble(textBox1.Text);
            f = "-";
            textBox1.Text = "";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            a = Convert.ToDouble(textBox1.Text);
            f = "*";
            textBox1.Text = "";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            a = Convert.ToDouble(textBox1.Text);
            f = "/";
            textBox1.Text = "";
        }

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

        private void button10_Click(object sender, EventArgs e)
        {
            a=Convert.ToDouble(textBox1.Text) ;
            f = "+";
            textBox1.Text = "";

        }
    }
}

注意连按数字的情况所以代码应为 textBox1.Text += “1”;
清零以后再其他按数字之前要先把0去掉,所以在按数字时需判断一下输入框是否为0

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值