C# 四則運算及省市選擇及日月選擇

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 = 0; //用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 == 0)
                {
                    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 = 1; i < 13; 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 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    comboBox5.Items.Clear();
                    for (int i = 1; i < 32; i++)
                    {
                        comboBox5.Items.Add(Convert.ToString(i));
                    }
                    break;
                case 2:
                    comboBox5.Items.Clear();
                    for (int i = 1; i < 30; i++)
                    {
                        comboBox5.Items.Add(Convert.ToString(i));
                    }
                    break;
                default:
                    comboBox5.Items.Clear();
                    for (int i = 1; i < 31; 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);
        }
    }
}

 

 

相關文章
相關標籤/搜索