Add value to ComboBox

Source: Internet
Author: User

In winform, unlike in Web, ComboBox (winform), dropdownlist (Web), and item

 

Different, the value of item cannot be directly added to winform, but the value of item can be added to web.

 

Write a ComboBox here to add the value.

 

Class dropdownlist: system. Windows. Forms. ComboBox
{
Private bool _ useitemvalue;

[System. componentmodel. Description ("whether to enable value. If so, call the bindvalue method after binding data")]
[System. componentmodel. Category ("data")]
[System. componentmodel. browsable (true)]
[System. componentmodel. defaultvalue (false)]
Public bool useitemvalue
{
Get {return _ useitemvalue ;}
Set {_ useitemvalue = value ;}
}

/// <Summary>
/// Bind the value to the item
/// </Summary>
/// <Param name = "Sep"> delimiter </param>
Public void bindvalue (char SEP)
{
# Region bind the value to the item
If (_ useitemvalue)
{
List <keyvaluepair <string, Object> dit = new list <keyvaluepair <string, Object> ();
Foreach (Object OBJ in this. Items)
{
String text = "";
If (obj is system. Data. datarowview)
{
TEXT = (system. Data. datarowview) OBJ). Row. itemarray [0]. tostring ();
}
Else
{
TEXT = obj. tostring ();
}
String [] value = text. Split (SEP );
Try
{
Dit. Add (New keyvaluepair <string, Object> (value [0], value [1]);
}
Catch
{
Dit. Add (New keyvaluepair <string, Object> (text ,""));
}
}
This. datasource = dit;
This. displaymember = "key ";
This. valuemember = "value ";
}
# Endregion
}
}

 

After successful generation, set the value of useitemvalue in "data" to true.

 

And call the bindvalue method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.