Home > Software engineering >  Pass value from a CEdit on one dialog box to another dialogs CEdit Control
Pass value from a CEdit on one dialog box to another dialogs CEdit Control

Time:08-04

I have a Browse file/print file path function and a parent/Child window, I need help in passing path printed on a EditControl from child(dialog2) to parent(dialog1)control edit box. please, HELP! See code below:

dialog1.cpp

   dialog2 Dlg;
   Dlg.DoModal();

    if (Dlg.DoModal() == IDOK)
    {
       //print folderPath from dialog2 CEdit2 to a CEdit1 control on dialog1.
       // DDX_Control(pDX, IDC_EDIT_BOX1, _cEditBox1);
    }

dialog2.cpp

void dialog2::OnBnClickedBrowse()
{
//Lines of Code
//Function related to the question 

    if (pidl != NULL)
    {
        SHGetPathFromIDList(pidl, path);
        SetCurrentDirectory(path);
        _cEditBox2.SetWindowText(path);  //prints selected file path on the edit control
        GetDlgItemText(IDC_EDIT_BOX2, folderPath); //Need to capture the path to print it to 
                                                // dialog1 Editbox1, where I'm struggling 
                                              //DDX_Control(pDX, IDC_EDIT_BOX2, _cEditBox2);
    }
void dialog2::OnBnClickedOk()
{
    CDialogEx::OnOK();

   //not sure what to do here 
   //to pass value to dialog1:EditBox1 in the (DoModal()==IDOK) function 

}

CodePudding user response:

  1. Add a button and an edit box (add variables - CEdit m_edit1 , CString m_strSendMessage);

  2. Create a new dialog, also add a button and an edit box (add variables - CEdit m_edit2 , CString m_strGetMessage);

  3. Add an OnInitDialog() to the sub-dialog: Select Class View->Select MFC Class Wizard->Virtual Function->Add OnInitDialog(), the following is the initialization code to display the assignment when the sub-window is opened.

     BOOL child::OnInitDialog()
     {
     CDialogEx::OnInitDialog();
     m_edit2.SetWindowTextW(m_strGetMessage);
     return TRUE;
     }
    

    4.Double-click the parent window button to add a message response function.

     void CMFCApplication16Dlg::OnBnClickedButton1()
     {
     CString str;
     m_edit1.GetWindowTextW(str);
     child *dlg = new child();
     dlg->m_strGetMessage = str;
     dlg->Create(IDD_DIALOG1);
     dlg->ShowWindow(SW_SHOWNORMAL);
     }
    

    5.Double-click the child window button to add a message response function.

    void child::OnBnClickedButton1() {

     CMFCApplication16Dlg *p = (CMFCApplication16Dlg*)GetParent();
     CString str;
     m_edit2.GetWindowTextW(str);
     p->m_edit1.SetWindowText(str);
    

    }

Result:

enter image description here

CodePudding user response:

Simply use CEdit's GetWindowText() and SetWindowText() methods, eg:

dialog2 Dlg;
if (Dlg.DoModal() == IDOK)
{
    CString text;
    Dlg._cEditBox2.GetWindowText(text);
    _cEditBox1.SetWindowText(text);
}
  • Related