VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • 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);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186

在这里插入图片描述 python基础教程 c#教程


相关教程