Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As stated by <a href="https://stackoverflow.com/questions/867514/make-listbox-items-have-a-different-value-than-item-text/867542#867542">the 1st answer</a>, the use of <code>DisplayMember</code> works whether you are using asp.net or winforms.</p> <p>And to comment a bit more, it also works if you are using the rather old fashion <code>Items.add</code> way of adding items to a <code>ListBox</code>.</p> <p>Just for fun, here is a simple demo of what you need (just create a new form and drop on it a ListBox and a Label):</p> <pre><code>public partial class Form1 : Form { class Customer { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return string.Format("{0} {1}", LastName, FirstName); } } public Form1() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); listBox1.DisplayMember = "LastName"; listBox1.DataSource = GetCustomers(); //listBox1.Items.AddRange(GetCustomers().ToArray()); } private IEnumerable&lt;Customer&gt; GetCustomers() { return new List&lt;Customer&gt;() { new Customer() { FirstName = "Gustav", LastName = "MAHLER" }, new Customer() { FirstName = "Johann Sebastian", LastName = "BACH" } }; } private void lb_SelectedIndexChanged(object sender, EventArgs e) { label1.Text = listBox1.SelectedItem.ToString(); } } </code></pre> <p>Enjoy</p> <p>PS: <a href="https://stackoverflow.com/questions/867514/make-listbox-items-have-a-different-value-than-item-text/867524#867524">@2nd post</a> <code>Tag</code> is not available to <code>ListBox</code>: because it accepts an array of <code>object</code>, not a specific item container like <code>ListView</code>... but you don't need any in your case. <code>Tag</code> is useful when you want to carry additional data along with a specific <code>TreeViewItem</code> or <code>ListViewItem</code> for example. By the way, <code>Tag</code> is defined at the <code>Control</code> level and so exists for <code>Button</code>, <code>Label</code>, and so on... but for my part I think it is rather a bad idea to store business data in it (untyped, UI coupled...) apart from the <code>ListView</code> and <code>TreeView</code> cases for which it is rather convenient.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload