vico
vico

Reputation: 18175

Get MFC dialog member variable content

I have dialog that contains Text Edit control binded to CEdit m_edit member variable. After show modal I need to get content of Text Edit.

BOOL CPreparationApp::InitInstance()

{


    MyDlg Dlg;

    m_pMainWnd = &Dlg;
    Dlg.DoModal();


    CString strLine;
     Dlg.m_edit.GetWindowTextW(strLine); // Debug assertion message

}

Durring Dlg.m_edit.GetWindowTextW(strLine); I have exception:

---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!

Program: C:\Windows\SYSTEM32\mfc110ud.dll
File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp
Line: 1215

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

---------------------------
Abort   Retry   Ignore   
---------------------------

What this exception means? How to copy string from m_edit?

Upvotes: 0

Views: 925

Answers (1)

Gautam Jain
Gautam Jain

Reputation: 6849

After DoModal, the edit box is destroyed. So you cannot access it.

You must save the text from edit box to a CString member variable in functions like OnOK(). I am assuming that you are having OnOK() method inside your dialog class.

Inside the dialog class you would have:

public:

CString m_editText;

In OnOK() you would write:

m_edit.GetWindowTextW(m_editText);

After calling DoModal, you can access the text using

Dlg.m_editText

You can improve the code here by having Get & Set functions for getting the m_editText value instead of accessing a public member variable m_editText (which is not a good design).

Upvotes: 1

Related Questions