c++ - Is there any way to add an object other than CString to a CComboBox in MFC? -


i trying add object has member variable cstring ccombobox. cannot add string because trying interface tool requires me have member variable other string list item in ccombobox. below trying do.

ccombobox::addstring(myownobject); 

i want string of myownobject display, whole object in listbox other member variable can reached other tool.

the ccombobox class wraps native combo box control. basic implementation meets common use case: displaying strings selection user.

if need additional functionality, can use ccomboboxex class instead. exposes full set of operations of underlying comboboxex control. in particular, items can configured retrieve string representation items @ runtime, based on arbitrary information.

the following assuming, custom item data layout follows:

struct customitemdata {     cstringw m_name;     int m_someinteger; }; 

the item data can arbitrarily complex, , hold information wish store. populating ccomboboxex items requires calling ccomboboxex::insertitem, passing appropriately filled comboboxexitem structure:

// customitemdata's lifetime must exceed of ccomboboxex; don't use // stack-based (automatic) variable. customitemdata* pcid = new customitemdata( myname, myinteger );  ccomboboxexitem cbei = { 0 }; cbei.mask = cbeif_text | cbeif_lparam; cbei.iitem = currentindex;  // zero-based index of item. cbei.psztext = lpstr_textcallback;  // control request information using                                     // cben_getdispinfo notification codes. cbei.lparam = reinterpret_cast<lparam>( pcid );  // assign custom data item. mycombobox.insertitem( &cbei ); 

at point, combobox control populated items, , request display information application. cben_getdispinfo sent control parent, notification handler must placed parent's window (usually dialog) implementation. handler connected notification message using on_notify macro:

// inside parent's message map: on_notify( cben_getdispinfo, idc_my_combobox, getcbdispstring )  // message handler inside parent's class void cmydlg::getcbdispstring( nmhdr* pnmhdr, lresult* presult ) {     nmcomboboxex* pncbe = reinterpret_cast<nmcomboboxex*>( pnmhdr );     comboboxexitem& cbei = pncbe->ceitem;     if ( cbei.mask & cbeif_text ) {         // text requested -> fill appropriate buffer.         const customitemdata& cd = *reinterpret_cast<const customitemdata*>( cbei.lparam );         wcscpy( cbei.psztext, cd.m_name );         // prevent future callbacks item. optional optimization         // , can used, if m_name member doesn't change.         cbei |= cbeif_di_setitem;     }     // mark notification handled     *presult = 0; } 


occasionally desired put cben_getdispinfo callback inside custom combobox implementation. mfc provides necessary infrastructure implement message reflection (see tn062: message reflection windows controls). allows parent window reflect notification messages respective child control handling. can useful @ times, not required implement solution question.


if not need full control on constructing display strings @ runtime, can go simple ccombobox control, , attach additional information calling ccombobox::setitemdata or ccombobox::setitemdataptr, illustrated in πάντα ῥεῖ's answer.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -