How does combobox know key/value?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

When using the .Add() method of a combo box, how does it know which
element in my object is the key and which is the value?

Say my object looks like this:

object1.field1 = "2";
object1.field2 = "Number2";

and I do:
combobox1.Items.Add(object1);

I'd like field2 displayed in the combo box and then some way to
reference its corresponding value. Will I need to create a look up
table to acheive this because field2 is all that will ever be returned
by the combo?

DataSource() won't be used here.

Thanks,
Brett
 
try combox1.DisplayMember = "field2" ; a similar field exist for value.

/LM
 
I'm asking how does the combo box know when only this syntax is used:

combobox1.Items.Add(object1);

Or does it actually have a key/value at that point?

Brett
 
Combobox will use the ToString call on our object to determine what value to
display. If you define a ToString method in your object to override the
default ToString method, then you can make sure that Combobox will display
the field you'd like. When an item is selected, you can use the selection
index to retrieve the actual object from the combobox.
HTH

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Once I retrieve my object with the two fields via the selection index,
I have to always cast it back to what ever it was to get at those two
fields right?

Brett
 
It use Reflection and the DisplayMember you specify to know what to display

/LM
 
Not so Luc. When you use the .Add() method, Display and Value members
are not used. They are used only with datasource.

Brett
 
Yes. The combobox stores objects and uses the ToString method to get the
displayable portion. You can put any object you want in a listbox or
combobox, as long as you can produce a string for the list to display. When
you get the object back our, you will need to cast it back to its original
type in order to directly access the fields in your code.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Right. Now, as for the ToString() method, which of the two fields will
it choose to send back if both are string fields? There must be some
logic going on there to decide field1 or field2. Does it matter if
the two fields in my object are int and string fields. Both of them
are also displayable. Which will ToString() send back now?

Brett
 
Brett Romero said:
Right. Now, as for the ToString() method, which of the two fields will
it choose to send back if both are string fields? There must be some
logic going on there to decide field1 or field2. Does it matter if
the two fields in my object are int and string fields. Both of them
are also displayable. Which will ToString() send back now?

Brett

Here comes the power of object oriented design... the system doesn't
decide... YOU DO.

You override the ToString method with your own. You pick the one to
display. You can even create a string that combines the values... whatever
you want.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Did you actually tried it?

I know, because I use it often like that.You just be sure to fill the
DisplayMember after you add your items.

/LM
 
And the same effect can be achieved by having the DisplayMember point to an
accessor in your object that return the string you want to show.

/LM
 
Right. Now, as for the ToString() method, which of the two fields will
it choose to send back if both are string fields? There must be some
logic going on there to decide field1 or field2. Does it matter if
the two fields in my object are int and string fields. Both of them
are also displayable. Which will ToString() send back now?

Brett

You will have to write code to get back what you want.

Sometimes a picture is worth a thousand words.

Try something like this, Brett:

private void MainForm_Load(object sender, EventArgs e)
{
// People is inherited from List<T>
PeopleList people = new PeopleList();

// add some person objects to the list
people.Add(new Person(1, "Fonda", "Hanoi", "Jane"));
people.Add(new Person(2, "Kerry", "John", "F"));

// bind the list to the list box
LearningListBox.DataSource = people;
}

private void LearningListBox_SelectedIndexChanged(object sender,
EventArgs e)
{
// get the person object from the selected item
Person person = (Person)LearningListBox.SelectedItem;
// get the data from the person object and use it the way you want
int id = person.ID;
string lastName = person.LastName;
string firstName = person.FirstName;
string middleName = person.MiddleName;
}

// here is the Person.ToString() method

public override string ToString()
{
return string.Format("{0}, {1} {2}", _current.lastName,
_current.firstName, _current.middleName);
}

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top