Using system;
Using system. collections. generic;
Using system. componentmodel;
Using system. data;
Using system. drawing;
Using system. text;
Using system. windows. forms;
Namespace winformcombobox
{
Public partial class frmmain: form
{
Public frmmain ()
{
Initializecomponent ();
}
# Region members
Arealists ac;
Public arealists gettestarea ()
{
Arealists al = new arealists ();
For (int I = 1; I <11; I ++)
{
Area a = new area ();
A. area_id = I. tostring ();
A. area_name = "" + I. tostring () + "name ";
Al. add ();
}
Return al;
}
# Endregion
# Region events
Private void frmmain_load (object sender, eventargs e)
{
Ac = gettestarea ();
Cblist. datasource = ac;
Cblist. displaymember = "area_name ";
Cblist. valuemember = "area_id ";
}
Private void btnsetcombobox_click (object sender, eventargs e)
{
//// Setvalue0 ();
// Setvaluebyvalue ();
//// Setvaluebyitem ();
Setvaluebytext ();
}
# Endregion
# Region methods
Private void setvalue0 ()
{
Area a = new area ();
A. area_id = "8 ";
A. area_name = "8th ";
Cblist. datasource = gettestarea ();
Cblist. displaymember = "area_name ";
Cblist. valuemember = "area_id ";
Cblist. selectedvalue = a. area_id;
Lbresult. text = cblist. selectedvalue. tostring ();
}
Private void setvaluebyvalue ()
{
Area a = new area ();
A. area_id = "8 ";
A. area_name = "8th ";
Cblist. selectedvalue = a. area_id;
Lbresult. text = cblist. selectedvalue. gettype (). tostring () + ":" + cblist. selectedvalue. tostring ();
}
Private void setvaluebyitem ()
{
Area a = new area ();
A. area_id = "8 ";
A. area_name = "8th ";
Cblist. selecteditem = ac. findall (delegate (area ar) {return ar. area_id = a. area_id;}) [0];
Lbresult. text = cblist. selecteditem. gettype (). tostring () + ":" + cblist. selectedvalue. tostring ();
}
Private void setvaluebytext ()
{
Area a = new area ();
A. area_id = "8 ";
A. area_name = "8th ";
Cblist. selectedindex = cblist. findstring (a. area_name );
Lbresult. text = cblist. selectedvalue. gettype (). tostring () + ":" + cblist. selectedvalue. tostring ();
}
# Endregion
}
}
Using system;
Using system. collections. generic;
Using system. text;
Namespace winformcombobox
{
# Region area
Public class area
{
Private string m_area_id;
Public string area_id
{
Get {return m_area_id ;}
Set {m_area_id = value ;}
}
Private string m_area_name;
Public string area_name
{
Get {return m_area_name ;}
Set {m_area_name = value ;}
}
Private double m_area_order;
Public double area_order
{
Get {return m_area_order ;}
Set {m_area_order = value ;}
}
}
# Endregion
[Serializable]
Public class arealists: list <area>
{
Private int _ maxitems = 0;
Public int maxitems {get {return this. _ maxitems;} set {this. _ maxitems = value ;}}
}
}