专业编程基础技术教程

网站首页 > 基础教程 正文

C#中switch选择语句配合下拉列表完成季节判断与窗体颜色变化代码

ccvgpt 2024-08-28 13:36:15 基础教程 17 ℃

基于《C#中switch选择语句配合下拉列表控件完成季节判断与窗体颜色变化》视频讲解源代码:

设计界面:

C#中switch选择语句配合下拉列表完成季节判断与窗体颜色变化代码

季节判断:

Bash
private void button1_Click(object sender, EventArgs e)
        {
            switch(comboBox1.SelectedIndex+1)
            {
                case 3:
                case 4:
                case 5:
                    textBox1.Text = "春季";
                    break;
                case 6:
                case 7:
                case 8:
                    textBox1.Text = "夏季";
                    break;
                case 9:
                case 10:
                case 11:
                    textBox1.Text = "秋季";
                    break;
                case 12:
                case 1:
                case 2:
                    textBox1.Text = "冬季";
                    break;
            }
        }

窗体背景颜色变化:

Bash
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(comboBox2.SelectedIndex)
            {                
                case 0:
                    this.BackColor=Color.Red;
                    break;                
                case 1:
                    this.BackColor = Color.Yellow;
                    break;                
                case 2:
                    this.BackColor = Color.Blue;
                    break;                
                case 3:
                    this.BackColor = Color.Green;
                    break;
                case 4:
                    this.BackColor = Color.Pink;
                break;
            }
        }

结语:喜欢的请关注!

最近发表
标签列表