【问题标题】:Get MFC dialog member variable content获取MFC对话框成员变量内容
【发布时间】:2014-04-03 05:39:10
【问题描述】:

我有一个对话框,其中包含绑定到 CEdit m_edit 成员变量的文本编辑控件。显示模式后,我需要获取文本编辑的内容。

BOOL CPreparationApp::InitInstance()

{


    MyDlg Dlg;

    m_pMainWnd = &Dlg;
    Dlg.DoModal();


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

}

Dlg.m_edit.GetWindowTextW(strLine);期间我有例外:

---------------------------
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   
---------------------------

这个异常意味着什么?如何从 m_edit 复制字符串?

【问题讨论】:

标签: c++ visual-c++ mfc


【解决方案1】:

DoModal 后,编辑框被销毁。所以你不能访问它。

您必须将编辑框中的文本保存到 OnOK() 等函数中的 CString 成员变量。我假设您的对话框类中有 OnOK() 方法。

在你将拥有的对话框类中:

public:

CString m_editText;

在 OnOK() 中你会写:

m_edit.GetWindowTextW(m_editText);

调用DoModal后,可以使用

Dlg.m_editText

您可以通过使用 Get & Set 函数来获取 m_editText 值而不是访问公共成员变量 m_editText(这不是一个好的设计)来改进此处的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多