Hi,
I have a MDI application and from the menu I can open a dialog with a edit control and a OK button. When I push the OK button the text in the edit control shall be stored in a variable in my CDocument class. But when the dialog has been closed, the variable that should store the CString is empty. What have I do wrong
In the dialog class I have this code:
void CMyDlg::OnBnClickedOk()
{
CString EditCtrlText;
m_EditCtrl.GetWindowTextA(EditCtrlText);
CMyDoc m_CallFunc;
m_CallFunc.StoreCStringInVariable(CString EditCtrlText); // Call the function in CDocument class
OnOK();
}
When the OK button is pushed it calls the 'StoreCStringInVariable()' function that is declared in the CDocument class and sends the EditCtrlText as parameter.
void CMyDoc::StoreCStringInVariable(CString strText)
{
m_CstringTxt = strText; // The 'm_CstringTxt' is the CString variable there the text should be stored. It is declared in the CDocument class.
}
After the function has been called is the 'm_CstringTxt' variable empty. I want the variable shall store the text even after the dialog has been closed. How would I do that
Help me plz!

Transfer CString between classes
ailleresc
Hmm... let's see
CMyDoc m_callFunc;
creates a new instance of the CMyDoc class so your code does not access the existing document instance. Moreover, this instance is local so it will be destroyed when the functions returns. Anything you modify in it will be lost. You will need probably to pass a pointer to the active document to this dialog when you create it so you can access it. For example if the code that show the dialog is in CMainFrame class then you can get a pointer to the active document using GetActiveDocument() function.
Then there is the line:
m_CallFunc.StoreCStringInVariable(CString EditCtrlText);
That should not even compile and even if it did it would be wrong.
It should be
m_CallFunc.StoreCStringInVariable(EditCtrlText);
Andrew312
It's the same thing. You are creating a new instance of the CMyDoc class. You need to use a pointer to the EXISTING INSTANCE of the document class.
JasonSD
Don't know if it helps but a document can be created by using the OnFileNew member of the CWinApp class. There are other ways to do ut but they are a bit complicated and I think they are undocumented.
Ivan Martinez
Jay890
hi,
now I have change the function call to this:
CMyDoc* m_CallFunc = new CMyDoc;
m_CallFunc->StoreCStringInVariable(EditCtrlText);
but it still doesn't work.
kishoremsn
You cannot use GetActiveDocument from a dialog because there is no active document then the dialog is open. You need to get the active document pointer before you display your dialog and pass the pointer to the dialog using a public field member or better by using a public method like
private:
CMyDoc *m_pDocument;
public:
void SetDocument(CMyDoc *pDoc)
{
m_pDocument = pDoc;
}
in the dialog class.
Let's say you are displaying the dialog from MainFrame menu then the code should look something like this:
void CMainFrame::OnViewMyDialog()
{
CMyDoc *pDoc = (CMyDoc *)GetActiveDocument();
if (pDoc == NULL)
return; // there is no active document
CMyDlg dlg;
dlg.SetDocument(pDoc);
dlg.DoModal();
}
Kuttappan
and how should I do that I don't know if I can use the GetActiveDocument function in a CDialog class. What kind of pointer should I use The class that the function 'OnBnClickedOk' is in is declared something like this:
class CMyDlg : public CDialog
{
public:
afx_msg void OnBnClickedOk();
afx_msg void OnFileNew();
CEdit m_EditCtrl;
protected:
virtual void DoDataExchange(CDataExchange* pDX);
};