Following the example provided for a list control (and changing things for a combo box) is okay if you know in advance how many such controls you wish to appear on your dialog box, and this number of list boxes is invariant.
I use Marcel's stuff to generate dialogs with from 0 (zero) to whatever number of combo box's on it. So extending CDynDialogEx is not much use as I don't know in advance how many combo boxes I need.
So to get around this I just add the combo boxes to the CDynDialogEx dialog as I need them (as shown in the CTestDynDialogDlg::OnButton2 example - but I include an extra parameter at the end which is a pointer to a CList<cstring *,="" cstring="" *=""> object.....
First I changed the following bits of code:-
DynDialogEx.h
virtual UINT AddDlgControl(DLGITEMTEMPLATECONTROLS TypeControl, LPCTSTR lpszCaption, DWORD dwStyle, DWORD dwExtendedStyle, LPRECT pRect = NULL, void *pData = NULL, UINT nID = 0, void *pData2 = 0);<br />
<br />
virtual UINT AddDlgControl(LPCSTR lpszClassName, LPCTSTR lpszCaption, DWORD dwStyle, DWORD dwExtendedStyle, LPRECT pRect = NULL, void *pData = NULL, UINT nID = 0, void *pData2 = 0);<br />
<br />
virtual UINT AddSubclassedDlgControl(LPCSTR lpszClassName, LPCTSTR lpszCaption, DWORD dwStyle, DWORD dwExtendedStyle, LPRECT pRect = NULL, UINT nID = 0, void *pData2 = 0);
and its implementation in DynDialogEx.cpp, for the 3 respective functions change their function definitions and then the 3 appropriate lines to be as follows....
<br />
nRet = pDynDialogItemEx->InitDialogItem(TypeControl, dwStyle, dwExtendedStyle, pRect, lpszCaption, nID, FALSE, pData, pData2);<br />
<br />
nRet = pDynDialogItemEx->InitDialogItem(lpszClassName, dwStyle, dwExtendedStyle, pRect, lpszCaption, nID, FALSE, pData, pData2);<br />
<br />
nRet = pDynDialogItemEx->InitDialogItem(lpszClassName, dwStyle, dwExtendedStyle, pRect, lpszCaption, nID, TRUE, NULL, pData2);<br />
DynDialogItemEx.h
Add a new member variable...
void *m_pData2;
and modify the 2 InitDialogItem function definitions....
UINT InitDialogItem(DLGITEMTEMPLATECONTROLS TypeControl, DWORD dwStyle, DWORD dwExtendedStyle, LPRECT pRect, LPCTSTR lpszCaption, UINT nID = 0, BOOL bSubclassed = FALSE, void *pData = NULL, void *pData2 = NULL);<br />
<br />
UINT InitDialogItem(LPCSTR lpszClassName, DWORD dwStyle, DWORD dwExtendedStyle, LPRECT pRect, LPCTSTR lpszCaption, UINT nID = 0, BOOL bSubclassed = FALSE, void *pData = NULL, void *pData2 = NULL);
In the corresponding implementation file DynDialogItemEx.cpp ensure that the function definitions match what you have just changed in the header file and add the following line to each function after the m_pData = pData; line....
m_pData2 = pData2;
Add some extra stuff in CDynDialogItemEx::DoDataExchange to populate the combobox with whatever is in m_pData2....
..<br />
.<br />
.<br />
CWnd::DoDataExchange(pDX);<br />
if(m_pData2 != NULL)<br />
{<br />
switch(m_eTypeControl)<br />
{<br />
case COMBOBOX:<br />
{
CList<CString *, CString *> *pListComboData = (CList<CString *, CString *> *)m_pData2;<br />
POSITION posComboData = pListComboData->GetHeadPosition();<br />
while(posComboData)<br />
{<br />
((CComboBox *)this)->AddString(*pListComboData->GetNext(posComboData));<br />
}<br />
break;<br />
}<br />
default:<br />
break;<br />
}<br />
}
Okay, that's the original code updated, now to make use of it.
<br />
CList<CList<CString *, CString *> *, CList<CString *, CString *> *> listComboBoxStringLists;<br />
<br />
CList<CString *, CString *> *pListComboBoxStrings = new CList<CString *, CString *>;<br />
<br />
CString *pNewString1 = new CString(_T("String 1"));<br />
CString *pNewString2 = new CString(_T("String 2"));<br />
pListComboBoxStrings.AddTail(pNewString1);<br />
pListComboBoxStrings.AddTail(pNewString2);<br />
<br />
listComboBoxStringLists.AddTail(pListComboBoxStrings);<br />
<br />
dlg.AddDlgControl(_T("COMBOBOX"), _T(""), STYLE_COMBOBOX_DROPDOWN, EXSTYLE_COMBOBOX, NULL, NULL, 0, pListComboBoxStrings);<br />
<br />
..<br />
.<br />
int nEditRtn = dlg.DoModal();<br />
if(nEditRtn == IDOK)<br />
{<br />
}<br />
<br />
POSITION posCStringList = listComboBoxStringLists.GetHeadPosition();<br />
while(posCStringList)<br />
{<br />
CList<CString *, CString *> *pListList = listComboBoxStringLists.GetNext(posCStringList);<br />
POSITION posCString = pListList->GetHeadPosition();<br />
while(posCString)<br />
{<br />
delete pListList->GetNext(posCString);<br />
}<br />
delete pListList;<br />
}<br />
------------------------------------------------------------------
I'm sorry this is so complicated but this is the only way I could come up with to achieve having a variable number of combo boxes on a dialog, and being able to add strings to the list.
There are bound to be syntax errors in this code, so please let me (and everyone else know).
"On this day at least 13 people fell off their bicycles, and some of the sea evaporated."
|