功能實現效果:
實現代碼:
1>聲明相關變量存儲專業、課程設置及相關信息:
[csharp]
string gr, msg, xy;
string[] spec, cour1, cour2, cour3, cour4, cour5;
2>在Form1類中自定義函數bool checkGR()檢查學生信息:
[csharp]
private bool checkGR()
{
bool check = true;
if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
{
check = false;
MessageBox.Show("姓名和年齡必須填寫");
}
return check;
}
3>在Form1類中自定義函數string record()收集學生信息:
[csharp]
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事件處理程序代碼:
[csharp]
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>組合框代碼設計:
[csharp]
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代碼:
[csharp]
private void button1_Click(object sender, EventArgs e)
{
if (checkGR())
{
MessageBox.Show(record(), textBox1.Text + "的推薦信息");
}
}
7>退出Button代碼:
[csharp]
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}