C#列表框、复选列表框、组合框的用法实例
程序员文章站
2023-12-14 15:15:04
本文实例讲述了c#列表框、复选列表框、组合框的用法。分享给大家供大家参考。具体分析如下:
功能实现效果如下图所示:
...
本文实例讲述了c#列表框、复选列表框、组合框的用法。分享给大家供大家参考。具体分析如下:
功能实现效果如下图所示:
实现代码:
1、声明相关变量存储专业、课程设置及相关信息:
复制代码 代码如下:
string gr, msg, xy;
string[] spec, cour1, cour2, cour3, cour4, cour5;
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;
}
{
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 + ">";
}
}
{
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;
}
{
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;
}
}
{
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 + "的推荐信息");
}
}
{
if (checkgr())
{
messagebox.show(record(), textbox1.text + "的推荐信息");
}
}
7、退出button代码:
复制代码 代码如下:
private void button2_click(object sender, eventargs e)
{
this.close();
}
{
this.close();
}
希望本文所述对大家的c#程序设计有所帮助。
推荐阅读
-
C#列表框、复选列表框、组合框的用法实例
-
C#列表框、复选列表框、组合框的用法实例
-
ligerUI---ListBox(列表框可移动的实例)
-
2.3 组合框(Combo Box)和列表框(List Box)使用实例
-
JavaScript的一些下拉列表框用法
-
多选列表框动态添加,移动,删除,全选等操作的简单实例
-
多选列表框动态添加,移动,删除,全选等操作的简单实例
-
javascript对下拉列表框(select)的操作实例讲解_javascript技巧
-
ligerUI---ListBox(列表框可移动的实例)
-
javascript对下拉列表框(select)的操作实例讲解_javascript技巧