C#列表框、復選列表框、組合框的用法實例。本站提示廣大學習愛好者:(C#列表框、復選列表框、組合框的用法實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#列表框、復選列表框、組合框的用法實例正文
本文實例講述了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();
}
願望本文所述對年夜家的C#法式設計有所贊助。