简体   繁体   English

获取MFC对话框成员变量内容

[英]Get MFC dialog member variable content

I have dialog that contains Text Edit control binded to CEdit m_edit member variable. 我有一个对话框,其中包含绑定到CEdit m_edit成员变量的Text Edit控件。 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); 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? 如何从m_edit复制字符串?

After DoModal, the edit box is destroyed. DoModal之后,编辑框将被销毁。 So you cannot access it. 因此您无法访问它。

You must save the text from edit box to a CString member variable in functions like OnOK(). 您必须将文本从编辑框保存到OnOK()之类的函数中的CString成员变量中。 I am assuming that you are having OnOK() method inside your dialog class. 我假设您在对话框类中具有OnOK()方法。

Inside the dialog class you would have: 在对话框类中,您将拥有:

public:

CString m_editText;

In OnOK() you would write: 在OnOK()中,您将编写:

m_edit.GetWindowTextW(m_editText);

After calling DoModal, you can access the text using 调用DoModal之后,您可以使用

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). 您可以通过具有用于获取m_editText值的Get&Set函数而不是访问公共成员变量m_editText(这不是一个好的设计)来在此处改进代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM