title: Is it safe to use ComboBox.DisplayMember without data binding?
body: <p>I've always understood that <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.displaymember.aspx" rel="nofollow">DisplayMember</a> and ValueMember are only supposed to be used when databinding a ComboBox using its DataSource property. </p>
  
  <p>But in some code I'm maintaing I've noticed that using a ComboBox' <code>DisplayMember</code> property <em>does</em> work without databinding. Setting the property determines what is shown in the combobox. Setting <code>ValueMember</code> does not seem to work though (it does not set <code>SelectedValue</code>).</p>
  
  <p>My question is: is it safe to use this behavior? Or is there a risk the behavior may change in upcoming versions of .NET?</p>
  
  <p><em>I know you'd normally override the <code>ToString</code> method. In the real code, the MyType class is not as simple as in my example. I'm not sure if it would be safe to override its <code>ToString</code> method.</em></p>
  
  <hr>
  
  <p>A small sample to show this behavior.</p>
  
  <pre><code>using System;
  using System.Windows.Forms;
  
  internal class Program {
      public class MyType {
          public string MyText { get; set; }
          public string MyValue { get; set; }
      }
  
      public class MyForm : Form {
          private readonly ComboBox _myComboBox;
  
          public MyForm() {
              _myComboBox = new ComboBox {DisplayMember = "MyText", ValueMember = "MyValue"};
              _myComboBox.Items.Add(new MyType {MyText = "First item", MyValue = "1"});
              _myComboBox.Items.Add(new MyType {MyText = "Second item", MyValue = "2"});
              _myComboBox.SelectedIndexChanged += _myComboBox_SelectedIndexChanged;
              Controls.Add(_myComboBox);
          }
  
          private void _myComboBox_SelectedIndexChanged(object sender, EventArgs e) {
              var cb = (ComboBox) sender;
              System.Diagnostics.Debug.WriteLine(
                  "Index: {0}, SelectedValue: {1}", cb.SelectedIndex, cb.SelectedValue);
          }
      }
  
      private static void Main() {
          Application.Run(new MyForm());
      }
  }
  </code></pre>