C#自定义泛型类绑定ComboBox控件
C# WinForm ComboBox 自定义数据项 (ComboBoxItem )
因为大家日常应用通常是键/值对的形式去绑定它的.
那么用键值对的形式如何做?
因为Combox的每一个项的值是一个object, 实际上就是一个键/值对.
我用的是下面这个类的实例作为它的一个项:
/// <summary>
/// ComboBox的项
/// </summary>
class ListItem : System.Object
{
private string m_sValue = string.Empty;
private string m_sText = string.Empty;
/// <summary>
/// 值
/// </summary>
public string Value
{
get { return this.m_sValue; }
}
/// <summary>
/// 显示的文本
/// </summary>
public string Text
{
get { return this.m_sText; }
}
public ListItem(string value, string text)
{
this.m_sValue = value;
this.m_sText = text;
}
public override string ToString()
{
return this.m_sText;
}
public override bool Equals(System.Object obj)
{
if (this.GetType().Equals(obj.GetType()))
{
ListItem that = (ListItem)obj;
return (this.m_sText.Equals(that.Value));
}
return false;
}
public override int GetHashCode()
{
return this.m_sValue.GetHashCode(); ;
}
}
通过这个类就可以定义ComboBox的值了, 首先我们定义一个ListItem的清单作为ComboBox的数据源:
List<ListItem> items = new List<ListItem>();
items.Add(new ListItem("0", "Item_0_Text"));
items.Add(new ListItem("1", "Item_1_Text"));
items.Add(new ListItem("2", "Item_2_Text"));
items.Add(new ListItem("3", "Item_3_Text"));
items.Add(new ListItem("4", "Item_4_Text"));
items.Add(new ListItem("5", "Item_5_Text"));
然后进行相应的设置:
//将数据源的属性与ComboBox的属性对应
drpTest.DisplayMember = "Text"; //显示
drpTest.ValueMember = "Value"; //值
然后进就可以进行绑定了:
drpTest.DataSource = items; //绑定数据
绑定数据之后, 就可以对其进行默认选择项的设置, 取值等操作:
drpTest.SelectedValue = "4"; //设定选择项
//取得当前选择的项
ListItem selectedItem = (ListItem)drpTest.SelectedItem;
string value = selectedItem.Value; //值
string text = selectedItem.Text; //显示的文字
其他操作大家就依样画葫芦吧. 呵呵.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication3
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
} public struct ComboBoxItem<TKey, TValue>
{
private TKey key;
private TValue value; public ComboBoxItem(TKey key, TValue value)
{
this.key = key;
this.value = value;
} public TKey Key
{
get { return key; }
} public TValue Value
{
get { return value; }
} public override string ToString()
{
return Value.ToString();
}
} private void Form3_Load(object sender, EventArgs e)
{
//KeyValuePair<int, string> keys = new KeyValuePair<int,string>();
this.comboBox1.Items.Add(new ComboBoxItem<int, string>(1, "Lin"));
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var item = (ComboBoxItem<int, string>)this.comboBox1.SelectedItem; Text = item.Value;
}
}
}
一个 1月 到12 月的下拉单
for (int i = 1; i <= 12; i++){ this.comboBox1.Items.Add( new ComboBoxItem<int, string>(i, String.Concat(i.ToString().PadLeft(2, '0'), "月")));} |
C#自定义泛型类绑定ComboBox控件的更多相关文章
- ExtJS基础知识总结:自定义日历和ComboBox控件(二)
概述 1.ExtJS 5不支持日期选择框中只选择年月,为了满足ExtJs5可以实现选择年月的功能,查询网上资料,整理出来了相应的处理方式,最终实现的效果如下图: 2.ExtJS 控件丰富,如果需要实现 ...
- comboBox控件动态绑定数据
/// <summary> /// load加载数据 /// </summary> /// <param name=" ...
- .Net平台Winform两个ComboBox控件绑定同一个数据源
今天WINFROM编程遇到这么一个问题:是有关WINFORM中两个comboBox控件绑定同一个数据源的问题,在窗体的界面上有两个comboBox,我在Form1_Load中对他们做了数据绑定(具体代 ...
- ComboBox控件绑定数据源
最近在研究机房收费系统的组合查询的方法时,看到了ComboBox控件可以进行数据绑定,我觉得这个功能真的很不错,可以给我省去很多的麻烦. 下面是我组合查询窗体界面 一.数据转换方法 现在我们开看一下我 ...
- Winform开发中如何将数据库字段绑定到ComboBox控件
最近开始自己动手写一个财务分析软件,由于自己也是刚学.Net不久,所以自己写的的时候遇到了很多问题,希望通过博客把一些印象深刻的问题记录下来. Winform开发中如何将数据库字段绑定到ComboBo ...
- ComboBox控件绑定数据源后,添加'请选择'或'全部'
ComboBox控件绑定数据源后,添加'请选择'或'全部' 当使用ComboBox控件绑定数据源之后,通过Items 属性添加的数据是无效的,此时如果要在所有选项前添加 选项 ,则需要考虑从数据源下手 ...
- winform combobox控件绑定 分类: WinForm 2014-04-17 14:34 118人阅读 评论(0) 收藏
想要达到的效果:把数据库中的一列数据绑定到combobox控件中. 数据库表:T_Task//任务表 列名:Task_Name//名称 主键:Task_ID combobox控件名称:cbName 解 ...
- C#中combobox 控件属性、事件、方法
一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...
- android - 自定义(组合)控件 + 自定义控件外观
转载:http://www.cnblogs.com/bill-joy/archive/2012/04/26/2471831.html android - 自定义(组合)控件 + 自定义控件外观 A ...
随机推荐
- 【实习记】2014-08-24实习生无法映射磁盘替代方案rsync+非默认端口22设置
正职开发人员有两个电脑,一个办公网的,一个开发网的.通过samba服务在开发网机器上映射编译环境机的磁盘没有问题. 开发岗实习生使用虚拟机做跳板方式登录编译环境机.上面的方法不能用. 替代方法:rsy ...
- 练习SignalR使用
前言 随着Ajax越来越普遍的使用,前端页面跟后台服务也越来越密切的进行交互,实现前后端进行实时的消息传递尤为重要,一文件上传为例,现在普遍使用ajax上传然后通过flash进行文件进度的显示,这是目 ...
- gnuplot使用
直接用yum安装gnuplot即可,例如 sudo sh -c "yum install gnuplot.x86_64 " 安装以后就可以使用了 编写gnuplot脚本 # grp ...
- JS实现继承多态
//类对象构造模版,无new访问,类似静态访问 var Class = { create: function () { return function () { //initialize初始化 //a ...
- jquery学会的
1.$("#id") $("xxxxx") (input, body) $(".class") 2. $("#id xxx ...
- [BZOJ 1047] [HAOI2007] 理想的正方形 【单调队列】
题目链接:BZOJ - 1047 题目分析 使用单调队列在 O(n^2) 的时间内求出每个 n * n 正方形的最大值,最小值.然后就可以直接统计答案了. 横向有 a 个单调队列(代码中是 Q[1] ...
- matlab拟合三维椭球
同学问的,查了下资料. %需要拟合的点的坐标为(0,-174.802,990.048),(0.472,-171.284,995.463),(0.413,-168.639,1003.55 ...
- ld: symbol dyld_stub_binding_helper not found, normally in crt1.o/dylib1.o/bundle1.o for architecture i386
就是选择的运行版本太低了,点击项目,project,把iOS DeployMent Target改为比较高的版本就行
- wireshark设置抓服务器的包
wireshark设置抓服务器的包:
- bzoj1391
很像最大权闭合子图的题目s向每个工作连边,流量为收益每个工序,由工作i向对应机器连边,流量为租用费每个机器向t连边,流量为购买费显然跑最小割,ans=总收益-mincut ; type node=re ...