• 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);
            }
        }
    }

    运行截图:

  • 相关阅读:
    后端——BA与图优化
    非线性优化
    后端卡尔曼滤波
    用HBuilderX把vue项目打包成apk
    vue中引入外部字体并使用
    使用iOS APP .mobileconfig套壳生成iosapp
    SCA组件识别
    赛事纪录 NOI 2022 统一省选
    Solution 「keyence2019_e 」Connecting Cities
    Open Cup 资源整理
  • 原文地址:https://www.cnblogs.com/xingyunblog/p/3888290.html
Copyright © 2020-2023  润新知