• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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

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.
0
lapucca
Asked:
lapucca
  • 2
  • 2
1 Solution
 
rstaveleyCommented:
You can use CComboBox's SetItemDataPtr/GetItemDataPtr to point to associated data.
0
 
lapuccaAuthor Commented:
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.
0
 
jkrCommented:
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.
0
 
lapuccaAuthor Commented:
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.
0
 
jkrCommented:
>>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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now