I am using a custom ListItem class in order to fill a windows forms combobox (in DropDownList style). I am filling the control fine, but how do I get the id (NOT the name) upon cmbProgramName_SelectedIndexChanged?

public class ListItem
        {
            private string id = string.Empty;
            private string name = string.Empty;

            public ListItem(string sid, string sname)
            {
                id = sid;
                name = sname;
            
            public override string ToString()
            {
                return this.name;
            }
            public string ID
            {
                get
                {
                    return this.id;
                }
                set
                {
                    this.id = value;
                }
            }
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    this.name = value;
                }
            }
            
        }

Look at the example here ListControl.ValueMember Property

Note: Your ListItem constructor is missing a closing } .

Set up the combo like this.

// create a list of ListItem objects to use as the ComboBox.DataSource
            List<ListItem> listitems = new List<ListItem>();
            listitems.Add(new ListItem("ID1", "Name1"));
            listitems.Add(new ListItem("ID2", "Name2"));
            listitems.Add(new ListItem("ID3", "Name3"));
            listitems.Add(new ListItem("ID4", "Name4"));
            listitems.Add(new ListItem("ID5", "Name5"));

            comboBox1.DisplayMember = "Name"; //<-- if you do this you do not need the ToString method in your ListItem.
            comboBox1.ValueMember = "ID"; //<-- this property type does not have to be string
            comboBox1.DataSource = listitems; //<-- must set DataSource to a list or array

Get the value using the SelectedValue property.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label3.Text = comboBox1.SelectedValue as string;
        }

Note: The SelectedValue property does not work if you just add to the combo using comboBox1.Items.Add .
You must set the DataSource property using a list or array.

commented: Deep knowledge. +7

If you wish to use comboBox1.Items.Add then get the ID like this.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem item = comboBox1.SelectedItem as ListItem;
            if (item != null)
            {
                label3.Text = item.ID;
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.