The way that you've written it, you will never be able to find out the id. The reason is that after you created the new
Member
object, you didn't maintain the reference to it. You added an item to the
comboBox
, but you just added a clone of the string.
So there are a couple of ways to do this. One is to override the
ToString
method of the
Member
class itself. As in:
class Member
{
protected override string ToString()
{
return this.FirstName;
}
}
Then, instead of adding
aMember.FirstName.ToString()
to the
ComboBox
, you add the
Member
object as in
this.comboBox1.Items.Add(aMember);
. Then, when an item is selected you could just set the
TextBox
to
((Member)(comboBox1.SelectedItem)).ID
. And, the
combobox
will use the
ToString
method to set the selected text and the text for each drop down.
Or, you could use a
BindingList<t></t>
. In that case, you create a new list like:
BindingList<Member> comboBoxList = new BindingList<Member>();
Then add the new items to the
BindingList
.
Then, you set the
comboBox1
binding properties as in:
comboBox1.DataSource = comboBoxList;
comboBox1.DisplayMember = "FirstName";
comboBox1.ValueMember = "ID";
(You just have to make sure that FirstName and ID are actually properties and not just public fields.)
Then in the
SelectedIndexChanged
event, you set the
TextBox
Text
field to
comboBox1.SelectedValue.ToString()
.