Combobox implements text and value

Source: Internet
Author: User

Combobox must have text and value ~

Steps:

1. Set the data source for combobox ~

ComboBox1.DataSource = list;

List is the data source you want to give to comboBox.

 

2. Set the text value for comboxBox: comboBox1.DisplayMember = "name"; // all the name field attributes in the set are text

 

3. Set the value of the header comboxBox: comboBox1.ValueMember = "id"; // the attribute of all id fields in the set is value
My list is a generic set ~

List <User> list = new List <User> ();

// Test Data

List. Add (new User (1, "qq "));

List. Add (new User (2, "bb "));

My User class is the following code (remember to set get method)

Public class User

{

Private int id;

Public int Id

{

Get {return id ;}

Set {id = value ;}

}

Private string name;

Public string Name

{

Get {return name ;}

Set {name = value ;}

}

Public User (){}

Public User (int id, string name)

{This. Id = id;

This. Name = name;

}

}
The following describes how to obtain text and value.

Get value MessageBox. Show (comboBox1.SelectedValue. ToString ());

Get text MessageBox. Show (comboBox1.Text );

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.