473,320 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 20780
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Faisal | last post by:
I have a question regarding the "PublicKeyToken" attribute of the "<section>" tag in the web.config file. Pretty much most references have this value set to "b77a5c561934e089"; the same value as in...
1
by: Tim Begin | last post by:
I am attempting to use the ThreadPool.SetMinThreads method as shown in the MSDN example code: int minWorker, minIOPort; int newWorker,newIOPort; ThreadPool.GetAvailableThreads(out minWorker, out...
3
by: Mephistopheles | last post by:
>>"Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message >>news >>:%23N6vQmpSFHA.3184@TK2MSFTNGP14.phx.gbl... >> Hi, >> I have a function already but I need to solve one...
5
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right...
15
by: Pucca | last post by:
I'm getting an error when I tried to use this BerConverter class in my C# code. Even though the Interent doc says that it runs on Win2000 sp4, I just thgouth I'll double check. Does anyone know...
5
by: needin4mation | last post by:
Learning about delegates (again, I admit), I think I finally get it, maybe. I can reference any method in any class in the same namespace as long as it has the same signature. Right? But how...
1
by: JSievers | last post by:
Hallo. A good friend of me develops web suites, for example his own at: www.augenpunkte.de. As you can see there my friend is blind and also he uses a Braille-line to develop these projects. For...
4
by: hhak | last post by:
Hi all, Im pretty new to C#, im trying to assign a combobox selected value to a int variable. int number; combobox has items added (1,2,3,4,...9) when i select a number from the combobox i...
3
by: DR | last post by:
I heard there is some trick to referencing statics in C# CLR stored procedure without having to mark the assembly as unsafe. Does anyone know this? This is usefull as the case of needing a little...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.