473,698 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding references to objects in a comboBox

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.GetSelectedI ndex();
MyObj selctedObj = reinterpret_cas t<MyObj >(cb.GetItemDat a(selectedIndex ));

Is there any way to store a reference to the object in a C# ComboBox?
Nov 16 '05 #1
3 11222
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******@hotma il.com> escribió en el mensaje
news:um******** ********@TK2MSF TNGP14.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.GetSelectedI ndex();
MyObj selctedObj = reinterpret_cas t<MyObj
(cb.GetItemDat a(selectedIndex ));

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

Nov 16 '05 #2
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******** ******@TK2MSFTN GP10.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******@hotma il.com> escribió en el mensaje
news:um******** ********@TK2MSF TNGP14.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.GetSelectedI ndex();
MyObj selctedObj = reinterpret_cas t<MyObj
(cb.GetItemDa ta(selectedInde x ));

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


Nov 16 '05 #3
"zfeld" <zf******@hotma il.com> wrote in message
news:eH******** ******@TK2MSFTN GP10.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.Dis playMember :
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3060
by: Mendhak | last post by:
Hi All, When you add an object to a combobox, and run the application, you'll get something like this: System.Windows.Forms.Button, Text: Button1 (for example). Is it possible to have just the text/value in the object to be displayed in
5
4657
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound combo by allowing the user to dynamically type in the new value directly in the combo box whcih in turn updates the lookup table and then when they have finished leaves that particular cell on the grid with the correct value. Seems...
1
1413
by: Marco Roberto | last post by:
Hi, is there anyway to add itens into a combobox after this control be bound with a dataset? Regards, Marco
6
3123
by: Juan Pedro Gonzalez | last post by:
I wanted to add a Combobox to a toolbar... Kind of the look you get on VisualStudio's toolbar. I've been able to find some VB 6 samples, but the placeholder option is no longer available for ToolbarButtons... Does anyone know how this could be done? Looking forward to hear from you,
0
1243
by: MIGUEL | last post by:
Hi all! Be patient because what I'm going to explain all of you it's more than very strange. I've developed a webservice project that contains two classes. One of them is going to act as a SOAP header, and the other class is the main class containing all the web methods. Besides, the project contains two references pointing at two normal DLLs built also with VS .NET 2003. This references are needed because the web methods use them in
47
3352
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols are objects whose representation within the code is more important than their actual value. Two symbols needs only to be
0
1898
by: marcobx | last post by:
I have a ComboBox and a List of objects to popolate items. I use the DataSource property and not the List collection: List<foolst = new List<foo>; //add elements into the list //... comboBox1.DataSource = lst; This work fine, items appear. Now I have a button near the combo box and I want to add a new element
0
1136
by: Matthew87 | last post by:
Hi all I'm having a bit of trouble completeing my coursework basically so far i have created Citenary class with support methods for a private array. The problem is i can add objects to the array through a instance of the class in the main form but im having trouble retreving the data from a combobox. I have the values Name, Passport Number, Issuedby and Expires that are added to the array. The value name is passed to the combobox when a...
6
1686
by: Ken Foskey | last post by:
I am brand new to coding C# and Visual Studio. I have worked out how to configure the ComboBox creating my own object however it leave me with a lot of management updating as the table changes. I would like to add to the table, so if you think sport, a game is played in a stadium that is located in a country. game has one venue has one country
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9028
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.