Is there a way to store 2 wstring values to each list item of a comboBox in a COM project?

Posted on 2006-04-30
Last Modified: 2013-11-25
Hi, I read in a name field and a description field for each list item of a comboBox on my Dialog page.  I want to display only the name in the Combobox but I want to be able to get its coorsponding despcritpion in the code.  Do I need to buid an array and deal with that each time my code need the descrption field of a selected name in the comboBox or is there a way to store it and access it easiy from the comboBox.  I know C# .net 's comboBox offers this feature but what about C++ comboBox?  Thanks.
Question by:lapucca
    LVL 17

    Expert Comment

    You can use CComboBox's SetItemDataPtr/GetItemDataPtr to point to associated data.

    Author Comment

    I'm not using MFC in my project.  Can you show an example or a url for a example how I can do this?  Thank you.
    LVL 86

    Expert Comment

    Qithout MFC, that would be like

    wstring pwstr = new wstring;

    *pwstr = "test";

    SendMessage(hComboBox,CB_SETITEMDATA, nItemIndex, (LPARAM) pwstr);

    However, you will need to take care of the pointers by deleting them later.

    Author Comment

    Thank you But I don't understand why I need to use "new" for the wstring pwstr.  Doesn't wstring manages it's own memory allocat and de-allocate?  

    Oh, Is this becuase the memory is allocated until I'm done with this.  Like I can release this at the end in my desctructor module.  How do I delete a wstring?  So far, I have let it manage its own memory.  

    Thank you.
    LVL 86

    Accepted Solution

    >>But I don't understand why I need to use "new" for the wstring pwstr.

    If you used a locally declared variable, it would be lost after it went out of scope, since you can only set a data pointer. Thus, you have to allocate the wstring on the heap in order to keep it valid.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now