Proper implementation of ComboBox
-
MFC - Ok I have a combobox (m_cFilters) and in it I have Filters like: All Files (*.*) Text Files (*.tx) ...etc. I want to grab the part between the parenthesis and store it in a var after the combo box is changed (DropDown combo, btw). I tried mapping the standard combo messages but after the functions return they do not update the combo box. What is the proper way to get the effect I am looking for? Is there an easier way to grab data like this other than straight from the combo string? Thanks. ---- Xian
-
MFC - Ok I have a combobox (m_cFilters) and in it I have Filters like: All Files (*.*) Text Files (*.tx) ...etc. I want to grab the part between the parenthesis and store it in a var after the combo box is changed (DropDown combo, btw). I tried mapping the standard combo messages but after the functions return they do not update the combo box. What is the proper way to get the effect I am looking for? Is there an easier way to grab data like this other than straight from the combo string? Thanks. ---- Xian
I haven't done much work with combo box controls, but I had a similar situation with a list control where i needed to store extra data besides what is displayed. I think you can use the CComboBoxEx object which use the COMBOEXITEM structure instead of LVITEM. You can store a pointer to a filter string (or a pointer to a structure if you need to store more)
typedef struct { TCHAR szFilter[256]; //eg "*.* } FILTERDATA, *PFILTERDATA;
Then just add the structure when adding your items, for list control:PFILTERDATA pData = new FILTERDATA; strcpy(pData->szFilter, szMyFilter); COMBOEXITEM item; memset(&item, 0, sizeof(item)); item.mask = CBEIF_LPARAm | CBEIF_TEXT; item.lparam = (LPARAM) pData; item.iItem = -1; item.pszText = new TCHAR[sizeof(szMyDisplay)]; item.cchText = sizeof(szMyDisplay); nItem = m_cFilters.InsertItem(0, &item);
Then when your user selects something do:COMBOEXITEM cbi; ZeroMemory(&cbi, sizeof(COMBOEXITEM)); cbi.iItem = hitTest.iItem; cbi.mask = CBEIF_PARAM; m_cFilters.GetItem(&cbi); PFILTERDATA pTemp = cbi.lParam; CString strTemp; strTemp.Format("Filter is %s", pTemp->szFilter);
or maybe there's an easier way :) btw don't forget to delete the lparam pointer. probably best to subclass the entire control so you can delete the memory in teh destructor