I am trying to set an edit box in a nested dialog, but the program crashes at runtime. I have made the folowing changes to make connection with the second dialog:
1) made a member variable in the 1st dialog of type the second class (derived from CDialog)
2) in the OnInitDialog() of the 1s class I have:
CRect rcDlg;
m_dDlgData.Create(CDialogData::IDD, this); // Create the second dialog
GetWindowRect(rcDlg);
m_dDlgData.SetWindowPos(NULL, 0, 0, rcDlg.Width(), rcDlg.Height(), SWP_NOZORDER);
3) created a CEdit variable in the second class, which is public in order to access it from the 1st class.
4) in an event handler of a button (in the 1st dialog), I want to make the given edit box (in the second dialog) read-only, and in another event handler to disable the read-only property. Here is the code, where the error happens:
void CZad1SemovDlg::OnBedit()
{
m_dDlgData.EGNReadOnly(true);//no problems here
///some code///
if(m_dDlgData.DoModal() == IDOK)// <-- the error happens here
{
//more code
}
}
bool CDialogData::EGNReadOnly(bool check)
{
m_cEGN.SetReadOnly(check);//here nothing strange happens, the variable is
//initialised
return true;
}
Strange, the control and the dialog variable are initialised, but when I run the DoModal() method, the program crashes. The error is: Debug Assertion Failed! dlgcore.cpp at line 492
Please, help, i am struggling with this for a while now, searched the net, but could not find the reason. I did not include the whole code as I thought it would be too much, but if you want me to add a function that you think I have missed, tell me and I will add it to the thread. I am using MSVC2008
When you call .DoModal(), it tries to create the dialog again which you already created by calling m_dDlgData.Create(). The assertion is telling you that the dialog is already created. Instead of calling DoModal(), call m_dDlgData.ShowWindow(SW_SHOW) and it should work. However, ShowWindow() will display the dialog modeless, but it should give you what you want.
EDIT: To achieve your goal, in the class for DlgData add a var to indicate edit mode. In the constructor, pass it an initializer:
When you need to display it, just call it:
This stops you from knowing too much about how the dialog implements “read-only”.
Hope this helps you.