带Key,Value实现的Combobox

struct itemEx
        {
            
public object Value;
            
public string Text;
            
public itemEx(object value, string text)
            {
                
this.Value = value;
                
this.Text = text;
            }
            
//重点是override ToString 
            public override string ToString()
            { 
return Text; }
        }

 

 

ComboBoxItemCollection coll = comboBoxEdit1.Properties.Items;
            coll.BeginUpdate();
            
try
            {
                coll.Add(
new itemEx("Sven""Petersen"));
                coll.Add(
new itemEx("Cheryl""Saylor"));
                coll.Add(
new itemEx("Dirk""Luchte"));
            }
            
finally
            {
                coll.EndUpdate();
                comboBoxEdit1.SelectedIndex 
= -1;
            }

 

 

private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            itemEx item 
= new itemEx();
            item 
= (itemEx)(this.comboBoxEdit1.SelectedItem);
            
string strValue = item.Value.ToString().Trim();
        }

 

posted @ 2011-03-27 12:17  kenter  阅读(534)  评论(0编辑  收藏  举报