C# 四则运算及省市选择及日月选择
阅读原文时间:2023年07月11日阅读:1

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 WindowsFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Text = "If语法计算";
button2.Text = "switch语法计算";
label2.Text = "省";
label3.Text = "市";
}

    private void Button1\_Click(object sender, EventArgs e)  
    {  
        int text1 = Convert.ToInt32(textBox1.Text);  
        int text2 = Convert.ToInt32(textBox2.Text);  
        int text3 = ; //用if的时候需要对变量进行赋值,不然在if语句里会报错  
        if (comboBox1.Text == "+" && comboBox1.SelectedItem == "+")  
        //由上一行可以看出combox的text和selecteditem的值一样,  
        //但是建议使用后者,所以好的写法如下面的  
        {  
            text3 = text1 + text2;  
        }  
        else if (comboBox1.Text == "-")  
        {  
            text3 = text1 - text2;  
        }  
        else if (comboBox1.Text == "\*")  
        {  
            text3 = text1 \* text2;  
        }  
        else if (comboBox1.Text == "/")  
        {  
            if (text2 == )  
            {  
                MessageBox.Show("除数不能为0");  
                return;  
            }  
            else  
            {  
                text3 = text1 / text2;  
            }  
        }  
        else  
        {  
            MessageBox.Show("不知道该怎么计算");  
        }  
        textBox3.Text = Convert.ToString(text3);  
    }

    private void Button2\_Click(object sender, EventArgs e)  
    {  
        try  
        {  
            int text1 = Convert.ToInt32(textBox1.Text);  
            int text2 = Convert.ToInt32(textBox2.Text);  
            int text3;//switch时候就不需要先对变量赋值  
            switch (comboBox1.SelectedItem)  
            {  
                case "+":  
                    text3 = text1 + text2;  
                    break;  
                case "-":  
                    text3 = text1 - text2;  
                    break;  
                case "\*":  
                    text3 = text1 \* text2;  
                    break;  
                case "/":  
                    //if (text2 == 0)  
                    //{  
                    //    MessageBox.Show("除数不能为0");  
                    //    return;  
                    //}  
                    text3 = text1 / text2;  
                    break;  
                default:  
                    MessageBox.Show("不知道该怎么计算");  
                    return;  
                    //throw new Exception("不知道怎么计算");也可以用这个  
            }  
            textBox3.Text = Convert.ToString(text3);  
        }  
        catch (Exception ex)  
        {

            MessageBox.Show("程序出现意外" + ex.Message);  
        }

    }  
    private void ComboBox2\_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        if (comboBox2.SelectedItem == "江苏省")  
        {  
            comboBox3.Items.Clear();//每次选择省时,清空市的内容  
            comboBox3.Items.Add("苏州");  
            comboBox3.Items.Add("张家港");  
            comboBox3.Items.Add("昆山");  
            comboBox3.Items.Add("吴江");  
        }  
        if (comboBox2.SelectedItem == "山东省")  
        {  
            comboBox3.Items.Clear();  
            comboBox3.Items.Add("青岛1");  
            comboBox3.Items.Add("青岛2");  
            comboBox3.Items.Add("青岛3");  
            comboBox3.Items.Add("青岛4");  
        }  
        if (comboBox2.SelectedItem == "浙江省")  
        {  
            comboBox3.Items.Clear();  
            comboBox3.Items.Add("杭州");  
            comboBox3.Items.Add("义乌");  
            comboBox3.Items.Add("温州");  
            comboBox3.Items.Add("台州");  
        }  
    }  
    private void ComboBox3\_SelectedIndexChanged(object sender, EventArgs e)  
    {

    }

    private void Form1\_Load(object sender, EventArgs e)  
    //初始化月份的下拉选择项  
    {  
        comboBox4.DropDownStyle = ComboBoxStyle.DropDownList;  
        comboBox4.Items.Clear();  
        for (int i = ; i < ; i++)  
        {  
            //comboBox4.Items.Add(i.ToString());//这个也是类型转换  
            comboBox4.Items.Add(Convert.ToString(i));  
        }  
    }  
    private void ComboBox4\_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        switch (Convert.ToInt32(comboBox4.SelectedItem))  
        {  
            case :  
            case :  
            case :  
            case :  
            case :  
            case :  
            case :  
                comboBox5.Items.Clear();  
                for (int i = ; i < ; i++)  
                {  
                    comboBox5.Items.Add(Convert.ToString(i));  
                }  
                break;  
            case :  
                comboBox5.Items.Clear();  
                for (int i = ; i < ; i++)  
                {  
                    comboBox5.Items.Add(Convert.ToString(i));  
                }  
                break;  
            default:  
                comboBox5.Items.Clear();  
                for (int i = ; i < ; i++)  
                {  
                    comboBox5.Items.Add(Convert.ToString(i));  
                }  
                break;  
        }  
    }  
    private void Button3\_Click(object sender, EventArgs e)  
    {  
        string str = string.Format("你选择了{0}月{1}日", comboBox4.SelectedItem, comboBox5.SelectedItem);  
        MessageBox.Show(str);  
        string str1 = comboBox4.SelectedItem + "月" + comboBox5.SelectedItem + "日";  
        MessageBox.Show(str1);  
    }  
}  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章