469,275 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

How does combobox know key/value?

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

Feb 17 '06 #1
12 20504
try combox1.DisplayMember = "field2" ; a similar field exist for value.

/LM

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Feb 18 '06 #2
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

Feb 18 '06 #3
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.
--
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Feb 18 '06 #4
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

Feb 18 '06 #5
It use Reflection and the DisplayMember you specify to know what to display

/LM

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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

Feb 18 '06 #6
Not so Luc. When you use the .Add() method, Display and Value members
are not used. They are used only with datasource.

Brett

Feb 18 '06 #7
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.
--
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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

Feb 18 '06 #8
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

Feb 18 '06 #9
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.
--
Feb 19 '06 #10
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

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Not so Luc. When you use the .Add() method, Display and Value members
are not used. They are used only with datasource.

Brett

Feb 19 '06 #11
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

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:se******************************@comcast.com. ..
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.
--

Feb 19 '06 #12
On 18 Feb 2006 13:43:51 -0800, "Brett Romero" <ac*****@cygen.com>
wrote:
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
Feb 19 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mephistopheles | last post: by
5 posts views Thread by Dmitriy Lapshin [C# / .NET MVP] | last post: by
5 posts views Thread by needin4mation | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.