How to save the filename retrieved from a dialog class using CMFCEditBrowseCtrl?
-
Hello! I'm trying to browse a text file using CMFCEditBrowseCtrl (it is used in CFirstDialog Class) and use this file in another Dialog Class (named CSecondDialog Class). My problem is on how to save the filename retrieved from the CMFCEditBrowseCtrl when the CFirstDialog::OnEnChangeMfceditbrowse1() goes out of scope and then pass the filename to the CSecondDialog. Here is the code:
void CFirstDialog::OnEnChangeMfceditbrowse1()
{
CString str;
m_browser.GetWindowText(str); //CMFCEditBrowseCtrl m_browser (variable)
}void CSecondDialog::OnBnClickedOk()
{
CStdioFile fileSource("here I need to put the filename retrieved from the previous dialog class", CFile::modeRead);
CStdioFile fileDest;
CString strLine;
CString strFile;
int num = 1;while (fileSource.ReadString(strLine))
{
strFile.Format(L"over%d.txt", num++);
fileDest.Open(strFile, CFile::modeWrite);
fileDest.WriteString(strLine);
fileDest.Close();
}fileSource.Close();
}
-
Hello! I'm trying to browse a text file using CMFCEditBrowseCtrl (it is used in CFirstDialog Class) and use this file in another Dialog Class (named CSecondDialog Class). My problem is on how to save the filename retrieved from the CMFCEditBrowseCtrl when the CFirstDialog::OnEnChangeMfceditbrowse1() goes out of scope and then pass the filename to the CSecondDialog. Here is the code:
void CFirstDialog::OnEnChangeMfceditbrowse1()
{
CString str;
m_browser.GetWindowText(str); //CMFCEditBrowseCtrl m_browser (variable)
}void CSecondDialog::OnBnClickedOk()
{
CStdioFile fileSource("here I need to put the filename retrieved from the previous dialog class", CFile::modeRead);
CStdioFile fileDest;
CString strLine;
CString strFile;
int num = 1;while (fileSource.ReadString(strLine))
{
strFile.Format(L"over%d.txt", num++);
fileDest.Open(strFile, CFile::modeWrite);
fileDest.WriteString(strLine);
fileDest.Close();
}fileSource.Close();
}
-
Hello! I'm trying to browse a text file using CMFCEditBrowseCtrl (it is used in CFirstDialog Class) and use this file in another Dialog Class (named CSecondDialog Class). My problem is on how to save the filename retrieved from the CMFCEditBrowseCtrl when the CFirstDialog::OnEnChangeMfceditbrowse1() goes out of scope and then pass the filename to the CSecondDialog. Here is the code:
void CFirstDialog::OnEnChangeMfceditbrowse1()
{
CString str;
m_browser.GetWindowText(str); //CMFCEditBrowseCtrl m_browser (variable)
}void CSecondDialog::OnBnClickedOk()
{
CStdioFile fileSource("here I need to put the filename retrieved from the previous dialog class", CFile::modeRead);
CStdioFile fileDest;
CString strLine;
CString strFile;
int num = 1;while (fileSource.ReadString(strLine))
{
strFile.Format(L"over%d.txt", num++);
fileDest.Open(strFile, CFile::modeWrite);
fileDest.WriteString(strLine);
fileDest.Close();
}fileSource.Close();
}
It depends on how your dialog classes can be accessed and if they both exist at the same time. A possible solution: You need to store pointers to your dialogs when they are created (usually in the parent window). Store the file name in a member variable of
CFirstDialog
and provide a public function to get the name:CFirstDialog
{
public:
// ...
const CString& GetFileName() const { return m_strFileName; }
protected:
// ...
CString m_strFileName;
};void CFirstDialog::OnEnChangeMfceditbrowse1()
{
m_browser.GetWindowText(m_strFileName);
}Similar for
CSecondDialog
but there with a set function:CSecondDialog
{
public:
// ...
void SetFileName(const CString& strFileName) { m_strFileName = strFileName; }
protected:
// ...
CString m_strFileName;
};You can now get and set the name from the class that holds the pointers to the dialogs. If both dialogs might not exist at the same time you have to store the file name in a member variable of that class (e.g. get the name when
DoModal()
returns). Finally you need access to the upper class fromCSecondDialog
. If the upper class is the parent window, always of the same type, and has been passed as parent, you can use casting:void CSecondDialog::OnBnClickedOk()
{
CParentOfDialogs *pParent = static_cast(GetParent());
// Use pParent here to get the stored name or retrieve it from the first dialog
CString strFileName = pParent->m_pFirstDialog->GetFileName();
}Otherwise you have to use a member variable in your second dialog that holds a pointer to the upper class and is initalised upon dialog creation or using a set function. There are also other solutions and variations of the above.
-
It depends on how your dialog classes can be accessed and if they both exist at the same time. A possible solution: You need to store pointers to your dialogs when they are created (usually in the parent window). Store the file name in a member variable of
CFirstDialog
and provide a public function to get the name:CFirstDialog
{
public:
// ...
const CString& GetFileName() const { return m_strFileName; }
protected:
// ...
CString m_strFileName;
};void CFirstDialog::OnEnChangeMfceditbrowse1()
{
m_browser.GetWindowText(m_strFileName);
}Similar for
CSecondDialog
but there with a set function:CSecondDialog
{
public:
// ...
void SetFileName(const CString& strFileName) { m_strFileName = strFileName; }
protected:
// ...
CString m_strFileName;
};You can now get and set the name from the class that holds the pointers to the dialogs. If both dialogs might not exist at the same time you have to store the file name in a member variable of that class (e.g. get the name when
DoModal()
returns). Finally you need access to the upper class fromCSecondDialog
. If the upper class is the parent window, always of the same type, and has been passed as parent, you can use casting:void CSecondDialog::OnBnClickedOk()
{
CParentOfDialogs *pParent = static_cast(GetParent());
// Use pParent here to get the stored name or retrieve it from the first dialog
CString strFileName = pParent->m_pFirstDialog->GetFileName();
}Otherwise you have to use a member variable in your second dialog that holds a pointer to the upper class and is initalised upon dialog creation or using a set function. There are also other solutions and variations of the above.
Is
m_pFirstDialog
a new variable?Jochen Arndt wrote:
If both dialogs might not exist at the same time you have to store the file name in a member variable of that class (e.g. get the name when
DoModal()
returns).The two dialogs do not exist at the same time, actually CSecondDialog gets on screen after I finish with CFirstDialog which goes out of scope. Could you explain me how to get the file name after
DoModal()
returns? Thank you in advance!void CInputView::OnFirstDlg()
{
// TODO: Add your command handler code here
CInputDoc* pDoc = GetDocument();
CFirstDialog DialogWindow;
//.....
CSecondDialog *pDlg = new CSecondDialog(this);
//...
pDlg->DoModal();
delete pDlg;
}void CMyView::OnSecondDlg()
{
// TODO: Add your command handler code here
CMyDoc* pDoc = GetDocument();
CSecondDialog DialogWindow;
DialogWindow.DoModal();}
-
Is
m_pFirstDialog
a new variable?Jochen Arndt wrote:
If both dialogs might not exist at the same time you have to store the file name in a member variable of that class (e.g. get the name when
DoModal()
returns).The two dialogs do not exist at the same time, actually CSecondDialog gets on screen after I finish with CFirstDialog which goes out of scope. Could you explain me how to get the file name after
DoModal()
returns? Thank you in advance!void CInputView::OnFirstDlg()
{
// TODO: Add your command handler code here
CInputDoc* pDoc = GetDocument();
CFirstDialog DialogWindow;
//.....
CSecondDialog *pDlg = new CSecondDialog(this);
//...
pDlg->DoModal();
delete pDlg;
}void CMyView::OnSecondDlg()
{
// TODO: Add your command handler code here
CMyDoc* pDoc = GetDocument();
CSecondDialog DialogWindow;
DialogWindow.DoModal();}
Add a member variable to the first dialog (as already described) and to the parent window. Then get the file name from the dialog after calling:
void CInputView::OnFirstDlg()
{
CFirstDialog DialogWindow;
DialogWindow.DoModal();
// Get file name here before DialogWindow goes out of scope
m_strFileName = DialogWindow.GetFileName();
// Or (when DialogWindow.m_strFileName is public):
//m_strFileName = DialogWindow.m_strFileName
}If you create the second dialog from another class (not
CInputView
), you have to pass the name to the other class in a similar way. Then set the file name before callingDoModal()
of the second dialog:CSecondDialog DialogWindow;
// Pass file name before calling DoModal()
DialogWindow.SetFileName(m_strFileName);
DialogWindow.DoModal(); -
Add a member variable to the first dialog (as already described) and to the parent window. Then get the file name from the dialog after calling:
void CInputView::OnFirstDlg()
{
CFirstDialog DialogWindow;
DialogWindow.DoModal();
// Get file name here before DialogWindow goes out of scope
m_strFileName = DialogWindow.GetFileName();
// Or (when DialogWindow.m_strFileName is public):
//m_strFileName = DialogWindow.m_strFileName
}If you create the second dialog from another class (not
CInputView
), you have to pass the name to the other class in a similar way. Then set the file name before callingDoModal()
of the second dialog:CSecondDialog DialogWindow;
// Pass file name before calling DoModal()
DialogWindow.SetFileName(m_strFileName);
DialogWindow.DoModal(); -
Add a member variable to the first dialog (as already described) and to the parent window. Then get the file name from the dialog after calling:
void CInputView::OnFirstDlg()
{
CFirstDialog DialogWindow;
DialogWindow.DoModal();
// Get file name here before DialogWindow goes out of scope
m_strFileName = DialogWindow.GetFileName();
// Or (when DialogWindow.m_strFileName is public):
//m_strFileName = DialogWindow.m_strFileName
}If you create the second dialog from another class (not
CInputView
), you have to pass the name to the other class in a similar way. Then set the file name before callingDoModal()
of the second dialog:CSecondDialog DialogWindow;
// Pass file name before calling DoModal()
DialogWindow.SetFileName(m_strFileName);
DialogWindow.DoModal(); -
Could you please explain me how to store pointers to the dialogs when they are created so that I can get and set the name from the class that holds the pointers to the dialogs? I'm sorry for asking again but I got a little confused with this part.
Such is only useful with modeless dialogs. Modal dialogs will block so that there is usually no need to have a pointer. However:
// With class member CSomeDialog *m_pSomeDlg
if (NULL == m_pSomeDlg)
{
m_pSomeDlg = new CSomeDialog(this);
}When doing so
m_pSomeDlg
must be initialised withNULL
in the constructor and deleted in the destructor.