1.C#WinForm基础制作简单计算器

利用c#语言编写简单计算器:

核心知识点:

 

 MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号
 MessageBox.Show(Convert.ToString(comboBox1.SelectedItem));//下拉内容
 MessageBox.Show(Convert.ToString(comboBox1.SelectedText));//数据库会用到
 MessageBox.Show(Convert.ToString(comboBox1.SelectedValue));//数据库会用到

 

 

源码如下:

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

namespace 简单计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)//加法
        {
            string str1 = textBox1.Text;//str1保存第一个文本框输入的内容
            string str2 = textBox2.Text;//str2保存第二个文本框输入的内容
            int i1, i2;
            if (!int.TryParse(str1, out i1))//等价于 if (!int.TryParse(str1, out i1)==false),将第一个文本框内容字符串转换成整型数据
            {
                MessageBox.Show("第一个数不是合法的整数");//弹出消息对话框
                return;//不要忘了return,仅仅退出所在的函数
            }
            if (int.TryParse(str2, out i2) == false)//将第二个文本框内容字符串转换成整型数据
            {
                MessageBox.Show("第二个数字不是合法的整数");//弹出消息对话框
                return;
            }
            int i3 = i1 + i2;//进行运算
            textBox3.Text = Convert.ToString(i3);// 等价于textBox3 = i3.ToString();
            
        }

        private void button2_Click(object sender, EventArgs e)//单击隐藏文本框
        {
            textBox1.Hide();//第一个文本框隐藏
            textBox2.Hide();//第二个文本框隐藏
            textBox3.Hide();//第三个文本框隐藏
            textBox4.Hide();
            textBox5.Hide();
            textBox6.Hide();
            textBox7.Hide();
            textBox8.Hide();
            textBox9.Hide();
            textBox10.Hide();
            textBox11.Hide();
            textBox12.Hide();
        }

        private void button3_Click(object sender, EventArgs e)//单击显示文本框
        {
            textBox1.Show();//第一个文本框显示
            textBox2.Show();//第二个文本框显示
            textBox3.Show();//第三个文本框显示
            textBox4.Show();
            textBox5.Show();
            textBox6.Show();
            textBox7.Show();
            textBox8.Show();
            textBox9.Show();
            textBox10.Show();
            textBox11.Show();
            textBox12.Show();

        }

        private void button4_Click(object sender, EventArgs e)//减法
        {
            string str3 = textBox4.Text;
            string str4 = textBox5.Text;
            int i3, i4;
            if (!int.TryParse(str3,out i3))
            { 
                MessageBox.Show("第一个数不是合法的整数");
                return;
            }
            if (!int.TryParse(str4,out i4)) 
            {
                MessageBox.Show("第二个数不是合法的数据");
            }
            int i5 = i3 -i4;
            textBox6.Text = Convert.ToString(i5);
        }

        private void button5_Click(object sender, EventArgs e)//乘法
        {
            string str3 = textBox7.Text;
            string str4 = textBox8.Text;
            int i3, i4;
            if (!int.TryParse(str3, out i3))
            {
                MessageBox.Show("第一个数不是合法的整数");
                return;
            }
            if (!int.TryParse(str4, out i4))
            {
                MessageBox.Show("第二个数不是合法的数据");
            }
            int i5 = i3 *i4;
            textBox9.Text = Convert.ToString(i5);

        }

        private void button6_Click(object sender, EventArgs e)//除法
        {
            string str3 = textBox10.Text;
            string str4 = textBox11.Text;
            int i3, i4;
            if (!int.TryParse(str3, out i3))
            {
                MessageBox.Show("第一个数不是合法的整数");
                return;
            }
            if (!int.TryParse(str4, out i4))
            {
                MessageBox.Show("第二个数不是合法的数据");
            }
            int i5 = i3 / i4;
            textBox12.Text = Convert.ToString(i5);
        }


    }
}

程序截图:

计算器版本2.0

源码如下:

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

namespace 计算器2._0
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           /********************************
            MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号
            MessageBox.Show(Convert.ToString(comboBox1.SelectedItem));//下拉内容
            MessageBox.Show(Convert.ToString(comboBox1.SelectedText));//数据库会用到
            MessageBox.Show(Convert.ToString(comboBox1.SelectedValue));//数据库会用到
            **********************************/
            string s1 = textBox1.Text;
            string s2 = textBox2.Text;
            int i1, i2;
            i1 = Convert.ToInt32(s1);
            i2 = Convert.ToInt32(s2);
            int result;
            switch (comboBox1.SelectedIndex) //多选框的序号
            {
                case 0:
                    result = i1 + i2;
                    break;
                case 1:
                    result = i1 - i2;
                    break;
                case 2:
                    result = i1 * i2;
                    break;
                case 3:
                    if (i2 == 0) //检查除数是否为零
                    {
                        MessageBox.Show("除数不能为零!!!");
                        return;
                    }
                    result = i1 / i2;
                    break;
                default://防患于未然
                    throw new Exception("未知的运算符");       
            }
            textBox3.Text = Convert.ToString(result);
        }
    }
}

运行截图:

 

转载于:https://www.cnblogs.com/xingyunblog/p/3888290.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值