The ComboBox in WinForm sets both the text and value methods

Source: Internet
Author: User

The ComboBox in WinForm cannot be set to the same value as the DropDownList control in the WebForm property, and can be implemented by writing a new class.

1, first add a new class ComboBoxItem in Form1:

public class ComboBoxItem
{
private string _text=null;
Private Object _value=null;
public string Text{get{return this._text;} set{this._text=value;}}
public object Value{get {return this._value;} Set{this._value=value;}}
public override string ToString ()
{
return this._text;
}
}

2. Add in the Form1_Load function:

ComboBoxItem newitem = new ComboBoxItem ();
NewItem. Text = "ABC";
NewItem. Value = "1";
COMBOBOX1.ITEMS.ADD (NewItem);

3. Add in ComboBox1_SelectedIndexChanged:

ComboBoxItem myitem = (comboboxitem) Combobox1.items[combobox1.selectedindex];
MessageBox.Show (MyItem.Value.ToString ());

You can see the output effect!!!

Upstairs, I also used it in the project.

C # code?

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647     /// <summary>    /// ComboBox的Item    /// </summary>    publicclassTextValue    {        publicTextValue() { }         publicTextValue(stringinText, intinValue)        {            this.Text = inText;            this.Value = inValue;        }        privatestring_text;        privateint_value;        /// <summary>        /// 文本        /// </summary>        publicstringText        {            setthis._text = value; }            getreturnthis._text; }        }        /// <summary>        /// 值        /// </summary>        publicintValue        {            setthis._value = value; }            getreturnthis._value; }        }    }  publicpartial classTempLateWave : Form{   List<TextValue> PurposeList = newList<TextValue>();    publicTempLateWave()    {        InitializeComponent();        PurposeList.Add(newTextValue("精度", 0));        PurposeList.Add(newTextValue("线性度", 1));        PurposeList.Add(newTextValue("一致性", 2));        cbx.DataSource = PurposeList;   //cbx 就是ComboBox控件        cbx.DisplayMember = "Text";        cbx.ValueMember = "Value";    }}

The ComboBox in WinForm sets both the text and value methods

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.