Combo Box Problem
-
Hello, Well I have this funny problem. I want to add items to my combo box in a child dialog box from my parent dialog box just before the child is shown. To do this I have the following line ChildDlg.m_ComboBox.AddString("Some Text"); I even tried ChildDlg.m_ComboBox.InsertString(0, "Some Text"); I made sure too that the member variable m_ComboBox in the class ChildDlg referred to the contro (not the value) of my Combo box. What is wrong? Thanks, Mike
-
Hello, Well I have this funny problem. I want to add items to my combo box in a child dialog box from my parent dialog box just before the child is shown. To do this I have the following line ChildDlg.m_ComboBox.AddString("Some Text"); I even tried ChildDlg.m_ComboBox.InsertString(0, "Some Text"); I made sure too that the member variable m_ComboBox in the class ChildDlg referred to the contro (not the value) of my Combo box. What is wrong? Thanks, Mike
Hi Mike, you have to tell us what the funny problem is. e.g. the app crashed (hm... thats really helpful), combo box not being updated, etc... Oh, and in which message handler are you calling the "AddString"/"InsertString" methods?
-
Hi Mike, you have to tell us what the funny problem is. e.g. the app crashed (hm... thats really helpful), combo box not being updated, etc... Oh, and in which message handler are you calling the "AddString"/"InsertString" methods?
Well the application crashes. I have a main dialog box and in one of the functions I create a child dialog box which has the member variable m_ComboBox as public. So in my main dialog box I have CChildDialog ChildDlg; ChildDlg.m_ComboBox.AddString("Some Text"); ChildDlg.DoModal(); And then when I run this application, it crashes. So to test again I tried to Add a string to the combobox in the constructor of my child dialog but the application still crashed! What shall I do?
-
Well the application crashes. I have a main dialog box and in one of the functions I create a child dialog box which has the member variable m_ComboBox as public. So in my main dialog box I have CChildDialog ChildDlg; ChildDlg.m_ComboBox.AddString("Some Text"); ChildDlg.DoModal(); And then when I run this application, it crashes. So to test again I tried to Add a string to the combobox in the constructor of my child dialog but the application still crashed! What shall I do?
Hello, the codegurus around the world.;) I think that DoModal() must call OnInitDialog() in CChildDialog. So, the location between ChilDlg and DoModal() will not create ComboBox yet. So, I think that you had better put AddString(...) in OnInitDialog() of CChildDialog.
CChildDialog ChildDlg;
ChildDlg.m_string = _T("Some Text");
ChildDlg.DoModal();And in OnInitDialog() of ChildDlg
//Already, m_string is updated!
m_ComboBox.AddString (m_string);Have a nice day!
-Masaaki Onishi-
-
Hello, the codegurus around the world.;) I think that DoModal() must call OnInitDialog() in CChildDialog. So, the location between ChilDlg and DoModal() will not create ComboBox yet. So, I think that you had better put AddString(...) in OnInitDialog() of CChildDialog.
CChildDialog ChildDlg;
ChildDlg.m_string = _T("Some Text");
ChildDlg.DoModal();And in OnInitDialog() of ChildDlg
//Already, m_string is updated!
m_ComboBox.AddString (m_string);Have a nice day!
-Masaaki Onishi-
-
Well the application crashes. I have a main dialog box and in one of the functions I create a child dialog box which has the member variable m_ComboBox as public. So in my main dialog box I have CChildDialog ChildDlg; ChildDlg.m_ComboBox.AddString("Some Text"); ChildDlg.DoModal(); And then when I run this application, it crashes. So to test again I tried to Add a string to the combobox in the constructor of my child dialog but the application still crashed! What shall I do?
Well, here's the problem... the control hasn't been created yet! MFC's UI classes are really wrappers around the Win32 equivalents. Having constructed the class does not imply that the corresponding controls have been created. So what you want to do is the following: (1) create a data member (a string or an array of strings maybe?) in your child dialog to hold the data thats going into the combo box. (2) set this data member after you have created the child dialog but before the call to
DoModal
(DUH!) (3) overrideOnInitDialog
of the child dialog (orOnCreate
if its not a dialog) to pass this data into the combo box usingAddString
orInsertString
, whichever you prefer. --update-- Oh bugger, Masaaki beat me to it..... ;p my link sux! :mad: -
Well, here's the problem... the control hasn't been created yet! MFC's UI classes are really wrappers around the Win32 equivalents. Having constructed the class does not imply that the corresponding controls have been created. So what you want to do is the following: (1) create a data member (a string or an array of strings maybe?) in your child dialog to hold the data thats going into the combo box. (2) set this data member after you have created the child dialog but before the call to
DoModal
(DUH!) (3) overrideOnInitDialog
of the child dialog (orOnCreate
if its not a dialog) to pass this data into the combo box usingAddString
orInsertString
, whichever you prefer. --update-- Oh bugger, Masaaki beat me to it..... ;p my link sux! :mad:Hey, I did as you told me. But now the application still mysteriously crashes. What I want to do is that just before the child appears, a function in the main dialog box (the parent) provides the child with stuff the child needs to show in the combo box. Since in the child I have a pointer to the parent, so I used it to call the function which should provide the child with the data for the combo box. But somehow the application crashes! Are we allowed to call other functions inside the OnInitDialog/OnInitInstance?
-
Hey, I did as you told me. But now the application still mysteriously crashes. What I want to do is that just before the child appears, a function in the main dialog box (the parent) provides the child with stuff the child needs to show in the combo box. Since in the child I have a pointer to the parent, so I used it to call the function which should provide the child with the data for the combo box. But somehow the application crashes! Are we allowed to call other functions inside the OnInitDialog/OnInitInstance?
Yes you are allowed to call other functions in those methods. But note that in your particular case, you should be using
OnInitDialog
instead ofOnInitInstance
-- sample pulled from MSDN -- IMPT NOTE: Remember to call the base class'OnInitDialog
as shown in the sample!!// MyDialog.cpp
#include "MyDialog.h"BOOL CMyDialog::OnInitDialog()
{
//call the base implementation first!
CDialog::OnInitDialog();// TODO: Add extra initialization here
m_cMyEdit.SetWindowText("My Name"); // Initialize control values
m_cMyList.ShowWindow(SW_HIDE); // Show or hide a control, etc.return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
} -
But there is no OnInitDialog() in my child dialog class!! Even when I tried to override OnInitDialog (hoping it was there in CDialog) it did not have it already defined in the base class.
Hello, the codegurus around the world.;) You can manually add OnInitDialog() with WM_INITDIALOG. Or use WM_INITDIALOG by the class wizard, and this automatically generates the message and its handler in CChildDlg class.:rolleyes: Have a nice day!
-Masaaki Onishi-
-
Hello, Well I have this funny problem. I want to add items to my combo box in a child dialog box from my parent dialog box just before the child is shown. To do this I have the following line ChildDlg.m_ComboBox.AddString("Some Text"); I even tried ChildDlg.m_ComboBox.InsertString(0, "Some Text"); I made sure too that the member variable m_ComboBox in the class ChildDlg referred to the contro (not the value) of my Combo box. What is wrong? Thanks, Mike
The other replies have been hit-and-miss, so I'll summarize here. You're confusing the dialog C++ object with the dialog window. The window does not get created until you call DoModal(). There is no dialog, and thus no controls to manipulate, before the DoModal() call. The proper way is to override OnInitDialog() in your dialog's C++ class. At the time WM_INITDIALOG is sent, the window and all child controls have been created, but not yet shown. So at that time, it's fine to set up the controls. --Mike-- http://home.inreach.com/mdunn/ While I can't be correct on all matters, I can make the reassuring claim that where I am inaccurate, I am at least definitively inaccurate. :love: your :bob: with :vegemite: and :beer: Sonork ID - 100.10414 AcidHelm
-
The other replies have been hit-and-miss, so I'll summarize here. You're confusing the dialog C++ object with the dialog window. The window does not get created until you call DoModal(). There is no dialog, and thus no controls to manipulate, before the DoModal() call. The proper way is to override OnInitDialog() in your dialog's C++ class. At the time WM_INITDIALOG is sent, the window and all child controls have been created, but not yet shown. So at that time, it's fine to set up the controls. --Mike-- http://home.inreach.com/mdunn/ While I can't be correct on all matters, I can make the reassuring claim that where I am inaccurate, I am at least definitively inaccurate. :love: your :bob: with :vegemite: and :beer: Sonork ID - 100.10414 AcidHelm
I don't understand why you don't fill the combo box after the DoModal()..It's not a properly way but it is sure.
-
I don't understand why you don't fill the combo box after the DoModal()..It's not a properly way but it is sure.
Hello, the codegurus around the world.;) Fisrt of all, we are talking about the initalization of the string in ComboBox. And, OnInitDialog() in the parent dialog of ComboBox is best location to do that. Second, the location after DoModal() means that ChildDlg will disapper since ChildDlg is the modal dialog like the message box. This means that as long as ChildDlg is opened (DoModal still works), we can add the new strings to this ComboBox inside or outside this dialog as long as we can get the right CWnd of ComboBox.:rolleyes: Have a nice day!
-Masaaki Onishi-