title: C# ComboBox with Text and Value
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2023316/c-sharp-winforms-combobox-with-label-and-value">C# Winforms Combobox with Label and Value</a>  </p>
  </blockquote>
  
  
  
  <p>How would one approach storing a display value and a real value in a ComboBox?</p>
  
  <p>Ie, the ComboBox displays:</p>
  
  <ul>
  <li>Destroy World</li>
  <li>Fire Slingshot</li>
  <li>Summon Cthulhu</li>
  </ul>
  
  <p>but the values as retrieved are:</p>
  
  <ul>
  <li>dw</li>
  <li>ss</li>
  <li>sc</li>
  </ul>
  
  <p>I want to be able to retrieve the value of the selected item in a way similar to this:</p>
  
  <pre><code>string selectedValue = combobox1.SelectedValue
  </code></pre>
  
  <h3>Updated code in response to answers:</h3>
  
  <pre><code>Dictionary&lt;string, string&gt; filterItems = new Dictionary&lt;string, string&gt;
  {
      {"Destroy World", "dw"},
      {"Fire Slingshot", "fs"},
      {"Summon Cthulu", "sc"},
  };
  this.options_filterby.DataSource = new BindingSource(filterItems, null);
  this.options_filterby.DisplayMember = "Key";
  this.options_filterby.ValueMember = "Value";
  </code></pre>
  
  <p>Now for some reason, although the DisplayMembers are absolutely fine, the ValueMembers return dictionary objects. Even stranger, after a while, eventually the ValueMembers will return strings as expected.</p>
  
  <pre><code>private void options_filterby_SelectedIndexChanged(object sender, EventArgs e)
  {
      MessageBox.Show(options_filterby.SelectedValue.ToString());
  }
  </code></pre>
  
  <p>This returns dictionaries for the first few times I change the selected item of the ComboBox, but eventually returns strings as needed.</p>
  
  <h3>Update: fixed (can't add as solution because question was closed)</h3>
  
  <p>In response to the above problem, the fix is to set the DisplayMember and ValueMember properties <em>before</em> the DataSource. I presume this is a bug.
  The code should read:</p>
  
  <pre><code>this.options_filterby.DisplayMember = "Key";
  this.options_filterby.ValueMember = "Value";
  this.options_filterby.DataSource = new BindingSource(filterItems, null);
  </code></pre>