日期:2014-05-18 浏览次数:21250 次
public class item { private object m_Value; private string m_Text; public item(object value, string text) { this.m_Value = value; this.m_Text = text; } public object Value { get { return m_Value; } } public string Text { get { return m_Text; } } public override string ToString() { return this.m_Text; } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); List<item> list = new List<item>(); for (int i = 0; i < 10; i++) { list.Add(new item(i, "item" + i.ToString())); } this.listBox1.DataSource = list; } protected override void OnClick(EventArgs e) { base.OnClick(e); item _item = this.listBox1.Items[this.listBox1.SelectedIndex] as item; if (_item != null) { Console.WriteLine(_item.Value); } }
------解决方案--------------------
System.Collections.Hashtable hash = new System.Collections.Hashtable();
for (int i = 0; i < 10; i++)
{
hash.Add(i.ToString(), "value:" + i.ToString());
}
//将HASH绑到LISTBOX
System.Collections.IDictionaryEnumerator myEnumerator = hash.GetEnumerator();
DataTable dtTemp=new DataTable();
dtTemp.Columns.Add("key",typeof(string));
dtTemp.Columns.Add("value",typeof(string));
DataRow drTemp=null;
while (myEnumerator.MoveNext())
{
drTemp=dtTemp.NewRow();
drTemp["key"]=myEnumerator.Key;
drTemp["value"]=myEnumerator.Value;
dtTemp.Rows.Add(drTemp);
}
this.listBox1.DataSource=dtTemp;
listBox1.DisplayMember="key";
listBox1.ValueMember="value";