windws form commbox.items.add()
来源:互联网 发布:淘宝上配送至什么意思 编辑:程序博客网 时间:2024/05/24 06:50
用comboBox的数据绑定的方法很简单,建一个数据源,绑定到ComboBox上,然后指定DisplayMember和 ValueMember就可以了。但是感觉好不灵活哦,如果我要在ComboBox上再添加一项,那怎么办?Web里面有ListItem, winform里面怎么没有了?感觉真是不爽,网上找了个方法,自己添加一个ListItem类,然后add到items里面,感觉还不错,有点象web 里面的用法了,可是问题又来了,添加的第一项怎么变成类名了?不是我给它赋的名字,其他项又都没有问题。于是又查到说,“因为combobox的 Item.ADD(一个任意类型的变量),而显示的时候调用的是这个变量的ToString()方法,如果这个类没有重载ToString(),那么显示的结果就是命名空间 + 类名”,于是加上重载的ToString()方法,好了,至此,我终于可以很方便的来给ComboBox和ListBox添加项了。
首先添加类ListItem:
/// <summary>
/// 选择项类,用于ComboBox或者ListBox添加项
/// </summary>
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;
}
}
}
然后在程序中使用:
ListItem item = new ListItem("我是值", "我是名字");
this.lbChoiceRoom.Items.Add(item);
this.lbChoiceRoom.DisplayMember = "Name";
this.lbChoiceRoom.ValueMember = "ID";
- windws form commbox.items.add()
- DropDownList3.Items.Add
- listView1.Items.Add()与 listView1.Items[i].SubItems.Add()区别
- DropDownList1.Items.Insert 与 DropDownList1.Items.Add 的区别
- Add Items to Telerik Ajax RadComboBox
- Commbox 控件
- add form and submit
- 學習 Tlist與指針的用法 用到了 Add, Count, Items
- 使用ComboBox.Items的Add方法添加列表项
- Add items to Ubuntu 12.04 Unity Launcher (quicklaunch)
- How to add dividers and spaces between items in RecyclerView?
- Writing CAML Queries from Retrieving List Items form SharePoint List
- Commbox的使用方法
- C# commbox数据绑定
- easyui commbox 二级联动
- before form submit ,add the validate check
- Drupal8 minimal Mode add comment form
- C#:ListBox的2个常用方法Add,Clear与Items的2个属性
- 在EasyDBO中使用Java注解配置映射的原理及使用
- SAP Java Connector(JCo) RFC
- Velocity脚本基本语法简介
- 嵌入开发(WinCE) 用C#设置系统时间
- AOP联盟的API简介
- windws form commbox.items.add()
- SCA与Spring框架
- 面试题:不用第三变量交换ab值
- 用AspectJ做的一个回合格斗小游戏
- ALV_GRID + 动态显示列
- 用HTTP实现手机与PC通讯
- 在EasyJWeb中使用Java Excel API 处理电子表格
- 在Spring中使用replaced-method来进行方法替换
- 开始全心投入《深入Spring 2:轻量级J2EE开发框架原理与实践》