C#列表框、复选列表框、组合框的使用

功能实现效果:

                      

实现代码:

1>声明相关变量存储专业、课程设置及相关信息:

 string gr, msg, xy;
 string[] spec, cour1, cour2, cour3, cour4, cour5;

2>在Form1类中自定义函数bool checkGR()检查学生信息:

private bool checkGR()
        {
            bool check = true;
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
            {
                check = false;
                MessageBox.Show("姓名和年龄必须填写");
            }
            return check;
        }

3>在Form1类中自定义函数string record()收集学生信息:

private string record()
        {
            gr = "";
            gr += textBox1.Text.Trim();
            if (radioButton1.Checked)
                gr += ",男";
            else
                gr += ",女";
            gr += "," + textBox2.Text.Trim() + "岁,";
            string hobby = "";
            foreach (Control ct1 in groupBox1.Controls)
            {
                CheckBox chk=(CheckBox)ct1;
                if (chk.Checked == true)
                {
                    hobby += "<" + chk.Text + ">";
                }
            }

            if (hobby != "")
            {
                hobby = "特长" + hobby;
            }
            gr += hobby;
            gr += ".\n";

            xy = "所学专业<" + comboBox1.Text+">.\n专业必修课程包括: ";
            foreach (object course in listBox1.Items)
            {
                xy += "<" + course.ToString() + ">";
            }
            xy += "。\n";
            if (checkedListBox1.CheckedItems.Count != 0)
            {
                xy += "特别说明:";
                foreach (object intr in checkedListBox1.CheckedItems)
                {
                    xy += "<" + intr.ToString() + ">";
                    xy += "。";
                }
            }
            
            msg += gr + xy;
            return msg;
        }

4>Load事件处理程序代码:

private void Form1_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
            spec = new string[5] { "计算机", "软件", "网络", "物联网", "通信" };
            comboBox1.DataSource = spec;
            comboBox1.SelectedIndex = 0;
            cour1 = new string[] {"计算机应用","程序设计","数据库","OPP","计网","计科" };
            cour2 = new string[] { "计算机应用", "程序设计", "数据库", "OPP", "计网", "高级编程" };
            cour3 = new string[] { "计算机应用", "程序设计", "无线网络安全", "OPP", "协议分析", "计科" };
            cour4 = new string[] { "计算机应用", "程序设计", "计算机数学", "数字媒体", "计网", "计科" };
            cour5 = new string[] { "计算机应用", "数电", "模电", "数学", "通信", "计科" };
            listBox1.DataSource = cour1;
        }

5>组合框代码设计:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(comboBox1.SelectedIndex)
            {
                case 0:
                    listBox1.DataSource = cour1;
                    break;
                case 1:
                    listBox1.DataSource = cour2;
                    break;
                case 2:
                    listBox1.DataSource = cour3;
                    break;
                case 3:
                    listBox1.DataSource = cour4;
                    break;
                case 4:
                    listBox1.DataSource=cour5;
                    break;
            }
        }

6>确定Button代码:

private void button1_Click(object sender, EventArgs e)
        {
            if (checkGR())
            {
                MessageBox.Show(record(), textBox1.Text + "的推荐信息");
            }
        }


7>退出Button代码:

private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值