c#简单版计算机实现加减乘除

①在视图的工具箱中给form1窗口添加如下:
在这里插入图片描述
②combobox1的属性ltems赋值

在这里插入图片描述
并页面加载时设置combobox1无法编辑,只读;
③button1的text属性赋值"=";
④textbox3设置readonly属性,赋值true,让textbox3为只读;
⑤代码:

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 _02计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //分别获取textbox1,textbox2;combobox1的内容用变量接收
            int a = int.Parse(textBox1.Text);
            int b = int.Parse(textBox2.Text);
            int fun = comboBox1.SelectedIndex;
            int res = 0;//定义一个变量用来接收计算的结果,并设置初始值为0;
            //根据索引判断combobox中选择的内容进行计算
            if (fun==0)
            {
                res = a + b;
            }
            if (fun==1)
            {
                res = a - b;
            }
            if (fun == 2)
            {
                res = a * b;
            }
            if (fun == 3)
            {
                res = a / b;
            }
            if (fun == 4)
            {
                res = a % b;
            }
            textBox3.Text = res.ToString();//将计算的结果给textbox3
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;//设置combobox1无法编辑,只读
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43434300/article/details/85269618