C#(WinForm) ComboBox和ListBox添加项
2015-08-01 15:00
239 查看
这篇文章主要介绍了C#(WinForm) ComboBox和ListBox添加项及设置默认选择项的的相关资料,需要的朋友可以参考下
dy(“bcall”);var cpro_id = “u1892994”;
Web控件DropDownList和WinForm控件ComboBox机制不一样。
ComboBox没有对应的ListItem需要自己写一个:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WinListItem
{
///
/// 选择项类,用于ComboBox或者ListBox添加项
///
public class ListItem
{
private string id = string.Empty;
private string name = string.Empty;
public ListItem(string sid, string sname)
{
id = sid;
name = sname;
}
public override string ToString()
{
return this.name;
}
public string ID
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
}
然后可以类似DropDownList添加项:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinListItem
{
public partial class MainFrm : Form
{
public MainFrm()
{
InitializeComponent();
}
}
}
运行如图:
参考:c#(winform)中ComboBox和ListBox添加项完全解决
刚开始用.net 的winform开发,发现好些控件都很难用,可能是不熟悉的原因吧,这不,一个给ComboBox添加项的问题就搞的我很头疼,我要同时给一个项添加名字和值,怎么都没法加,查了查资料,又自己汇总测试了下,终于全部搞定了,现把完整的方案写下。
用comboBox的数据绑定的方法很简单,建一个数据源,绑定到ComboBox上,然后指定DisplayMember和 ValueMember就可以了。但是感觉好不灵活哦,如果我要在ComboBox上再添加一项,那怎么办?Web里面有ListItem, winform里面怎么没有了?感觉真是不爽,网上找了个方法,自己添加一个ListItem类,然后add到items里面,感觉还不错,有点象web 里面的用法了,可是问题又来了,添加的第一项怎么变成类名了?不是我给它赋的名字,其他项又都没有问题。于是又查到说,“因为combobox的 Item.ADD(一个任意类型的变量),而显示的时候调用的是这个变量的ToString()方法,如果这个类没有重载ToString(),那么显示的结果就是命名空间 + 类名”,于是加上重载的ToString()方法,好了,至此,我终于可以很方便的来给ComboBox和ListBox添加项了。
复制代码 代码如下:
ListItem item = new ListItem(“我是值”, “我是名字”);
this.lbChoiceRoom.Items.Add(item);
this.lbChoiceRoom.DisplayMember = “Name”;
this.lbChoiceRoom.ValueMember = “ID”;
dy(“bcall”);var cpro_id = “u1892994”;
Web控件DropDownList和WinForm控件ComboBox机制不一样。
ComboBox没有对应的ListItem需要自己写一个:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WinListItem
{
///
/// 选择项类,用于ComboBox或者ListBox添加项
///
public class ListItem
{
private string id = string.Empty;
private string name = string.Empty;
public ListItem(string sid, string sname)
{
id = sid;
name = sname;
}
public override string ToString()
{
return this.name;
}
public string ID
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
}
然后可以类似DropDownList添加项:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinListItem
{
public partial class MainFrm : Form
{
public MainFrm()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e) { ListItem listItem = comboBox1.SelectedItem as ListItem; MessageBox.Show(listItem.ID + "," + listItem.Name); } private void MainFrm_Load(object sender, EventArgs e) { //添加项,Web控件DropDownList有对应的ListItem ListItem listItem0 = new ListItem("0", "选项零"); ListItem listItem1 = new ListItem("1", "选项一"); ListItem listItem2 = new ListItem("2", "选项二"); comboBox1.Items.Add(listItem0); comboBox1.Items.Add(listItem1); comboBox1.Items.Add(listItem2); //设置默认选择项,DropDownList会默认选择第一项。 comboBox1.SelectedIndex = 0;//设置第一项为默认选择项。 comboBox1.SelectedItem = listItem1;//设置指定的项为默认选择项 }
}
}
运行如图:
参考:c#(winform)中ComboBox和ListBox添加项完全解决
刚开始用.net 的winform开发,发现好些控件都很难用,可能是不熟悉的原因吧,这不,一个给ComboBox添加项的问题就搞的我很头疼,我要同时给一个项添加名字和值,怎么都没法加,查了查资料,又自己汇总测试了下,终于全部搞定了,现把完整的方案写下。
用comboBox的数据绑定的方法很简单,建一个数据源,绑定到ComboBox上,然后指定DisplayMember和 ValueMember就可以了。但是感觉好不灵活哦,如果我要在ComboBox上再添加一项,那怎么办?Web里面有ListItem, winform里面怎么没有了?感觉真是不爽,网上找了个方法,自己添加一个ListItem类,然后add到items里面,感觉还不错,有点象web 里面的用法了,可是问题又来了,添加的第一项怎么变成类名了?不是我给它赋的名字,其他项又都没有问题。于是又查到说,“因为combobox的 Item.ADD(一个任意类型的变量),而显示的时候调用的是这个变量的ToString()方法,如果这个类没有重载ToString(),那么显示的结果就是命名空间 + 类名”,于是加上重载的ToString()方法,好了,至此,我终于可以很方便的来给ComboBox和ListBox添加项了。
复制代码 代码如下:
ListItem item = new ListItem(“我是值”, “我是名字”);
this.lbChoiceRoom.Items.Add(item);
this.lbChoiceRoom.DisplayMember = “Name”;
this.lbChoiceRoom.ValueMember = “ID”;
相关文章推荐
- c#中子窗体中datagridview选中一条数据,把选中数据传回父窗体,使用tag
- C#属性、结构体、索引器、泛型
- C#自定义类型转换
- C#接口相关知识
- c#运算符重载
- c# winform DataGridView选择一整行的相关属性
- c#高级编程第七版 学习笔记 第二章 核心c#
- C# socket 局域网聊天程序的一点心得
- C#中的数组
- C# 自定义特性的使用
- C# 根据KeyEventArgs与组合键字符串相互转换
- C# 根据KeyEventArgs与组合键字符串相互转换
- C#结构体的特点浅析
- C#接口性能测试--计算执行时间
- C#中的线程
- 课堂随笔02--c#中string作为引用类型的特殊性
- 机房重构----C#中将DataTable类型转化为泛型集合
- visual studio 2013中常用的快捷键
- Windows Phone Silverlight 音乐进度条简单实现
- C#处理Excel