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() + "名";
al.add(a);
}
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 = "第8名";
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 = "第8名";
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 = "第8名";
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 = "第8名";
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; } }
}
}