By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

Adding references to objects in a comboBox

P: n/a
How do I add objects to a comboBox?
What I need is something similar to what there was in MFC as

MyObj obj;
ComboBox cb;

int index = cb.Add(obj.name);
cb.AddItemData(&obj, index);

Which can then be retrieved as:

int selectedIndex = cb.GetSelectedIndex();
MyObj selctedObj = reinterpret_cast<MyObj >(cb.GetItemData(selectedIndex ));

Is there any way to store a reference to the object in a C# ComboBox?
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
MyObj obj = new MyObj();
ComboBox cb = new ComboBox();
cb.Add(obj);

obj is a reference to your object.

You can get it as:

MyObj obj = (MyObj) cb.Item(index);

I hope this help you.
Bye.
Ernesto Lores

"zfeld" <zf******@hotmail.com> escribió en el mensaje
news:um****************@TK2MSFTNGP14.phx.gbl...
How do I add objects to a comboBox?
What I need is something similar to what there was in MFC as

MyObj obj;
ComboBox cb;

int index = cb.Add(obj.name);
cb.AddItemData(&obj, index);

Which can then be retrieved as:

int selectedIndex = cb.GetSelectedIndex();
MyObj selctedObj = reinterpret_cast<MyObj
(cb.GetItemData(selectedIndex ));

Is there any way to store a reference to the object in a C# ComboBox?

Nov 16 '05 #2

P: n/a
The problem with that, is it will display in the combo box the Tostring() of
MyObj.
The reason that this is a problem is MyObj is a complicated object with many
possible display strings (e.g its name, its author, its publisher, etc.)
The user was going to select vie a radiobutton what he wants to see
displayed in the ComboBox (names, authors, etc.)
I would re-populate the combo box with strings (MyObj.Name, MyObj.Author )
based on his choice but still have a reference to the underlying object when
one item is selected in the comboBox. I therefore can't let use the
ToString() for how would I know as to what to have the overridden ToString()
return (it might be MyObj.Name, or maybe MyObj.Author etc. )

Any other ideas?

"e-lores" <e_*****************@yahoo.es> wrote in message
news:Op**************@TK2MSFTNGP10.phx.gbl...
MyObj obj = new MyObj();
ComboBox cb = new ComboBox();
cb.Add(obj);

obj is a reference to your object.

You can get it as:

MyObj obj = (MyObj) cb.Item(index);

I hope this help you.
Bye.
Ernesto Lores

"zfeld" <zf******@hotmail.com> escribió en el mensaje
news:um****************@TK2MSFTNGP14.phx.gbl...
How do I add objects to a comboBox?
What I need is something similar to what there was in MFC as

MyObj obj;
ComboBox cb;

int index = cb.Add(obj.name);
cb.AddItemData(&obj, index);

Which can then be retrieved as:

int selectedIndex = cb.GetSelectedIndex();
MyObj selctedObj = reinterpret_cast<MyObj
(cb.GetItemData(selectedIndex ));

Is there any way to store a reference to the object in a C# ComboBox?


Nov 16 '05 #3

P: n/a
"zfeld" <zf******@hotmail.com> wrote in message
news:eH**************@TK2MSFTNGP10.phx.gbl...
The problem with that, is it will display in the combo box the Tostring() of MyObj.


What you want to do is to add a property to MyObj which returns, as a
string, the text you want displayed in the ComboBox for that object. Then
set the DisplayMember of the ComboBox to the name of that property. See the
example at ListControl.DisplayMember :
http://msdn.microsoft.com/library/de...embertopic.asp
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.