功能实现效果:
实现代码:
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(); }