Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

What are the DisplayMember and ValueMember?

You can fill a ComboBox with non-string items such as business objects.  By default, the ComboBox will call ToString() on each of the items to generate the display text (visible text).  Rather than rely on ToString(), you can have the ComboBox use one of the properties on the contained objects (business object) as the display text.  To do this, set the ComboBox.DisplayMember to the property name you want to use for the display text.  You can also set the ValueMember to a property on the contained objects that represents the object unique key.  You can then use the ComboBox "SelectedValue" property to set the ComboBox selected item via object key.

Sample: ComboBox DisplayMember and ValueMember (VS 2005) (VS Project: ComboBoxBinding)

/* Need to add a using statement for System.Globalization           */
/* Bind a ComboBox (culturesCB) to a current framework cultures     */
this.culturesCB.DisplayMember = "EnglishName";
this.culturesCB.ValueMember = "LCID";
this.culturesCB.DataSource = CultureInfo.GetCultures(CultureTypes.FrameworkCultures);

/* Set the currently selected item in the ComboBox                  */
this.culturesCB.SelectedValue = CultureInfo.CurrentCulture.LCID;