1.
By default, the ComboBox in winform is set to display the list in multiple texts, which is usually not in line with your daily applications,
Because daily applications usually bind key-value pairs.
So what should we do in the form of key-value pairs?
Because the value of each item in combox is an object, it is actually a key/value pair.
I use an instance of the following class as an item:
/// <Summary>
/// ComboBox item
/// </Summary>
Class listitem: system. Object
{
Private string _ value = string. empty;
Private string _ text = string. empty;
/// <Summary>
/// Value
/// </Summary>
Public String Value
{
Get {return this. _ value ;}
Set {This. _ value = value ;}
}
/// <Summary>
/// Displayed text
/// </Summary>
Public String text
{
Get {return this. _ text ;}
Set {This. _ text = value ;}
}
Public listitem (string value, string text)
{
This. _ value = value;
This. _ text = text;
}
Public override string tostring ()
{
Return this. _ text;
}
}
This class defines the value of ComboBox. First, we define a listitem list as the data source of ComboBox:
List items = new list ();
items. add (New listitem ("0", "item_0_text");
items. add (New listitem ("1", "item_shorttext");
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");
then set it accordingly:
// Maps the data source attributes to the ComboBox attributes.
Drptest. displaymember = "text"; // display
Drptest. valuemember = "value"; // Value
Then you can bind it:
Drptest. datasource = items; // bind data
After binding data, you can set the default selection items and values:
Drptest. selectedvalue = "4"; // set the selection item
// Obtain the selected items
Listitem selecteditem = (listitem) drptest. selecteditem;
String value = selecteditem. value; // Value
String text = selecteditem. Text; // displayed text
For other operations, just draw a gourd.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fcsh820/archive/2009/02/07/3867053.aspx
2.
Technorati tags: winform C #
In the web page, it is very convenient to use. However, when using C # Today, ComboBox can only display text with value, but does not support keys, when I add a large amount of data to the list, why?
No way, I can only do it myself. I studied ComboBox and added the object to add. It seems that there is a drama. if the process is not written, I only put the results. I can see it at a Glance:
Custom listitem
View plaincopy to clipboardprint?
- Public class listitem
- {
- Public String key {Get; set ;}
- Public String Value {Get; set ;}
- Public listitem (string pkey, string pvalue)
- {
- This. Key = pkey;
- This. value = pvalue;
- }
- Public override string tostring ()
- {
- Return this. Key;
- }
- }
When writing data:
View plaincopy to clipboardprint?
- Listitem li = new listitem ("name", "content ");
- This. combobox1.items. Add (LI );
- // N more
- This. combobox1.items. Add (LI );
- This. combobox1.displaymember = "key"; // The Key of the listitem.
- This. combobox1.valuemember = "value"; // corresponding to the value of listitem
When reading data:
- Listitem li = (listitem) combobox1.selecteditem;
- MessageBox. Show (Li. Key + Li. value );
Link: http://mdeve.com/blog/post/204
ComboBox
Technorati tags: winform C #
In the web page, it is very convenient to use. However, when using C # Today, ComboBox can only display text with value, but does not support keys, when I add a large amount of data to the list, why?
No way, I can only do it myself. I studied ComboBox and added the object to add. It seems that there is a drama. if the process is not written, I only put the results. I can see it at a Glance:
Custom listitem
View plaincopy to clipboardprint?
- Public class listitem
- {
- Public String key {Get; set ;}
- Public String Value {Get; set ;}
- Public listitem (string pkey, string pvalue)
- {
- This. Key = pkey;
- This. value = pvalue;
- }
- Public override string tostring ()
- {
- Return this. Key;
- }
- }
When writing data:
View plaincopy to clipboardprint?
- Listitem li = new listitem ("name", "content ");
- This. combobox1.items. Add (LI );
- // N more
- This. combobox1.items. Add (LI );
- This. combobox1.displaymember = "key"; // The Key of the listitem.
- This. combobox1.valuemember = "value"; // corresponding to the value of listitem
When reading data:
- Listitem li = (listitem) combobox1.selecteditem;
- MessageBox. Show (Li. Key + Li. value );
Link: http://mdeve.com/blog/post/204
ComboBox