|
Ho to add the string to Combo Box
Salman
Live and Let live
|
|
|
|
|
Did you ever find out how to add items to the combo box??? I'm hoping to do the same, but there isn't anything which indicates how to deal with the Combo box after you create it.
|
|
|
|
|
See the example of the Listbox.
It's basicly copy-paste-replace (List With ComboBox)
|
|
|
|
|
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."
|
|
|
|
|
Adding a callback function after the dynamic dialog has been instantiated will give
you much more flexibility - you will be able to get to all the dialog items via the
GetDlgItem() method and then you may modify your combo box (add/delete entries) and
perhaps SetCurSel(), etc.
In DynDialogEx.h:
class CDynDialogEx;<br />
typedef void (CALLBACK * CDynDialogExInitCBF)(CDynDialogEx * dlg);<br />
<br />
<br />
class CDynDialogEx : public CDialog<br />
{<br />
public:<br />
CDynDialogEx(CWnd * pParent = NULL, CDynDialogExInitCBF cbFunc = NULL);<br />
~CDynDialogEx();<br />
...<br />
...<br />
protected:<br />
...<br />
CDynDialogExInitCBF m_cbFunc;<br />
}
In DynDialogEx.cpp:
CDynDialogEx::CDynDialogEx(CWnd * pParent, <br />
CDynDialogExInitCBF cbFunc ) : CDialog()<br />
{<br />
...<br />
m_cbFunc = cbFunc;<br />
...<br />
}<br />
<br />
...<br />
<br />
BOOL CDynDialogEx::OnInitDialog()<br />
{<br />
...<br />
CDialog::OnInitDialog();<br />
if (m_cbFunc != NULL)<br />
{<br />
m_cbFunc(this);<br />
}<br />
CenterWindow();<br />
...<br />
}
In your own application:
void CALLBACK CondBPCallback(CDynDialogEx * dlg)<br />
{<br />
CComboBox * myCombo = (CComboBox *)dlg->GetDlgItem(IDC_MY_COMBOLISTBOX);<br />
if (myCombo != NULL)<br />
{<br />
myCombo->AddString(str0);<br />
myCombo->AddString(str1);<br />
myCombo->AddString(str2);<br />
myCombo->AddString(str3);<br />
myCombo->AddString(str4);<br />
myCombo->AddString(str5);<br />
myCombo->AddString(str6);<br />
myCombo->SetCurSel(3);<br />
}<br />
}
...
void CTestDynDialogDlg::OnButton1()
{
...
CDynDialogEx dlg(this, CondBPCallback);
..
}
That's all. There is no need to modify any other parts of this excellent utility.
Rudy Wang
|
|
|
|
|
Very Good!
Another question: Is it possible to add event handle function for controls, such as Button Click?
Thank you!
|
|
|
|
|
It does work! But I still don't know how to get the selected value of the Combo Box control.
|
|
|
|
|
hi Jordan Walters:
when I use there's code in DodataExchange.
((CComboBox *)this)->AddString(*pListComboData->GetNext(posComboData));
It's not works.
I move there's code in CreateEx function than it works.
so this code can't add in DoDataExChange funtion.
|
|
|
|
|
I think this is good work. F.e. it can be used for creation and distribution
some controls as lib projects.
Vitali
http://www.creative-case.com
|
|
|
|
|
Good, but perhaps a C++ implementation of the method Java AWT uses would have been better
|
|
|
|
|
Very nice!
A iactually coocked up something quite simmilar recently, but i ran across one big problem... How on Earth do you add a bitmap (preferebly from a file, not a resource) to a button???
|
|
|
|
|
Hi,
I would like to add a scrollbar when the number of controls added make the
dialog not fit on the screen. In the OnInitDialog method, I called EnableScrollBarCtrl,
and ShowScrollBar(SB_VERT,FALSE);
In the AddDlgControl method, I tried to do GetDlgRect, and if the rect.bottom-top
exceeds some limit - I tried ShowScrollBar(TRUE). This does not work as the m_hWnd
is 0? Any tips/hints/code would be great.
|
|
|
|
|
Hi,
Excuse my english, I´m a brazilian.
What can i do to use Tab Control ? If you have any source sample, i appreciate.
Thank´s for any help.
Mauro Cordon.
mcordon@usa.com
|
|
|
|
|
If there any way to deal with Tab Control?
|
|
|
|
|
I want to put a control, controled by a class included by me, in a dialog dynamicly created by DynDialogEx class. I think the subclassed controls are this, but after try to implementated I'm not sure. I Registered my class, and I create the control with the function AddSubclassedDlgControl. After that I execute the function SubclassDlgItem with de ID returned by AddSubclassedDlgControl, but my control is not created yet, in consecuence the control is not properly created in dynamic dialog.
Is possible add support for a new class in this class?. I seen the example CListDynDialogEx in DynamicDialog_demo, but I don't want a new dialog, and my control is from a other class!.
Any suggestion
Thanks in advance.
Amauta
|
|
|
|
|
One method which works for me (quick & dirty):
1. Add a new enum to the DLGITEMTEMPLATECONTROLS enum in DynDlgItemEx.h. I added USER_DEF at the end.
2. Alter a bit of the CDynDialogItemEx::CreateEx function to say:
else if (m_pData != NULL && IsDataMemberPointerToWnd ()) // exisiting code
{
// change call to pass NULL if classname is ""
bRet = ((CWnd*)m_pData)->CreateEx (m_dwStyleEx,
m_strClassName.GetLength () ? (LPCTSTR) m_strClassName : NULL,
m_strCaption,
m_dwStyle,
m_Rect,
pParent,
m_ControlID);
3. Add your pre-existing class instance (CWnd based but dont call Create ()) using
AddDlgControl (USER_DEF,......, pData...)
where pdata is a pointer to your instance
4. This works as follows: USER_DEF type results in a zero-length classname. If you detect the zero-length and substitute NULL, then CreateEx will cause a call thru to your CWnd class OnCreate handler, if you have set the pData paramter to point to your class.
Its rough, but it works for me. If you want to get fancier by actually using the classname of a class you have registered, a bit more work is involved, you probably need to add another AddDlgControl function that takes a classname and USER_DEF type, because the GetClassNameByType () function used in the exsiting code only recognises pre-defined types, so you'll always get an empty string.
Hope this helps
Chris
|
|
|
|
|
I dont know if I'm missing something, but the source and demo projects have the old versions. Where are the new ones?
|
|
|
|
|
The author resubmitted the downloads and I've updated them.
cheers,
Chris Maunder
Rub your belly and pat your head simultaneously. Sometimes that helps me make sense of things - Jon Sagara
|
|
|
|
|
How can I add a -ClistCtrl object- in the dynamic dialog?.
if I do:
OnButton(){
CListCtrl m_List;
CDynDialogEx dynDlg;
dynDlg.AddDlgControl(LISTCTRL, _T(""), WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER | LVS_SINGLESEL,0,rect,(void *) &m_List);
if (dynDlg.DoModal()== IDOK)){
MessageBox("OK");
}
}
I get a running error in the
DDX_Control(pDX, m_ControlID, *(CWnd*)m_pData); on DoDataExchange()
Any suggestion
Thanks
Tximist
|
|
|
|
|
I've got the same problem in a similar use case.
|
|
|
|
|
I simply add:
<br />
if (SingleSelection_)<br />
sListCtrl.CtlID = AddDlgControl(_T("SysListView32"), _T(""), LVS_SHOWSELALWAYS | LVS_REPORT | WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER | LVS_SINGLESEL, 0, pRect);<br />
else<br />
sListCtrl.CtlID = AddDlgControl(_T("SysListView32"), _T(""), LVS_SHOWSELALWAYS | LVS_REPORT | WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER, 0, pRect);<br />
I insert the CListCtrl's data in OnInitDialog().
I process the selected data in OnOK() using the saved CtlID.
|
|
|
|
|
Hi !
I try to use your classes but I cannot use the DDX with a CButton for example, or CWnd.
If I try to use in your example:
dlg.AddDlgControl(_T("BUTTON"), // Type of control OR classname of the control
_T("Press me!"),// Caption of control
STYLE_BUTTON, // dwStyle of control
EXSTYLE_BUTTON,// dwStyleEx of control
&rect, // Position of control on dialog in dialog units, default = NULL
(void*)&btn,// void pointer to variable use for DDX, default = NULL
IDC_DYN_BUTTON);// ID of the contol, default = zero
where btn is a CWnd, I have an ASSERT in wincore.cpp, saying that "only attach once, detach on destroy".
I tried to detach the controls, after createing, in CreateEx function, but it did not work.
I'll apreciate any help.
Thanks!
evolution_man.
|
|
|
|
|
Could anyone give me some hints on how to resize the dialog?
|
|
|
|
|
A easy way to resize the dialog. On "OnInitDialog" derived CDialog, use MoveWindow.
BOOL CDynDialogEx::OnInitDialog()
{
CDialog::OnInitDialog();
...
MoveWindow( x,y, width, height );
...
}
|
|
|
|
|
How can I use a listbox, and populate the list with text
|
|
|
|
|