0
votes

Currently writing a dialog based application in MFC that creates a child dialog when a button on the main dialog is pushed. The child dialog then has 3 edit control boxes and one button. When the button is pressed the information from the three edit control boxes is retrieved and stored as separate CStrings. Those CStrings are passed to a function located in the Main Dlg.cpp. That function takes the those strings and inserts them in a list control. The program builds fine and runs up until the add button(on the child dialog) is pressed. Not sure why the program is crashing I appear to have correct arguments?I'm also not sure if this is the best way to pass data like this between two dialog boxes. Any better ideas with that are also appreciated.

Popup Add Button Function

 void Popup::OnBnClickedPopadd()
   {
    CString str1, str2, str3;
    CThermotronDlg dlg;
    GetDlgItemText(ID_RampEdit,str1);
    GetDlgItemText(ID_DwellEdit,str2);
    GetDlgItemText(ID_TempEdit,str3);

    dlg.ReciveAndDisplay(str1,str2,str3);
   }

Receive and Display Function

 void CThermotronDlg::ReciveAndDisplay(CString str1, CString str2,CString str3){

    LPCTSTR ramp(str1),dwell(str2),temp(str3);

    MyListEx.InsertItem(1,ramp);
    MyListEx.SetItemText(0,1,dwell);
    MyListEx.SetItemText(0,2,temp);


}
1
I think your list control might not be initialized at the time ReciveAndDisplay is executed. You should try fetching string values from CThermotronDlg::OnInitDialog function.Igor Jerosimić
It might have something to do with the fact that you are adding your item at index 1 but then setting the item text for the item at index 0. Also, the LPCTSTR ramp(str1),dwell(str2),temp(str3); is unnecessary. Use str1, str2 and str3 directly.Nik Bougalis
@IgorJerosimić unlikely, since he says that the secondary dialog is created in response to a button being pressed on the main dialog (which hosts the list control), and OnInitDialog has already completed if the button was visible and could be pressed by the user.Nik Bougalis
@NikBougalis I thought he creates new dialog (CThermotronDlg dlg;) and sends data to it. Now that I have read it more carefully I think you right, but in that case I don't understand the purpose of CThermotronDlg dlg;Igor Jerosimić

1 Answers

1
votes

Instead of initialising the variable CThermotronDlg dlg; in your popup dialog, you need to locate the main dialog's address and call ReceiveAndDisplay method of this dialog directly. One of the way of doing this is by accessing the m_pMainWnd public variable of your application object (theApp). The proper code of OnBnClickedPopadd() will be:

 void Popup::OnBnClickedPopadd()
   {
    CString str1, str2, str3;
    CThermotronDlg *pDlg = (CThermotronDlg*)theApp.m_pMainWnd;
    GetDlgItemText(ID_RampEdit,str1);
    GetDlgItemText(ID_DwellEdit,str2);
    GetDlgItemText(ID_TempEdit,str3);

    pDlg->ReciveAndDisplay(str1,str2,str3);
   }