关闭模态MFC对话框后获取编辑框文本 [英] Getting edit box text from a modal MFC dialog after it is closed

查看:108
本文介绍了关闭模态MFC对话框后获取编辑框文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从模态MFC对话框中,我想在对话框关闭后从编辑框中提取文本.我尝试过:

From a modal MFC dialog, I want to extract text from an edit box after the dialog is closed. I attempted this:

CPreparationDlg Dlg;
CString m_str;

m_pMainWnd = &Dlg;
Dlg.DoModal();
CWnd *pMyDialog=AfxGetMainWnd();
CWnd *pWnd=pMyDialog->GetDlgItem(IDC_EDIT1);
pWnd->SetWindowText("huha max");
return TRUE;

它不起作用.

推荐答案

在您调用DoModal()之前,不会创建对话框及其控件,并且正如已经指出的那样,在DoModal()返回时已经销毁了该对话框及其控件.因此,您不能在DoModal()之前或之后都调用GetDlgItem().将数据传递或检索到控件的解决方案是在类中使用变量.您可以在创建类实例时在调用DoModal()之前进行设置.在OnInitDialog()中,将变量的值放入控件中.然后,在销毁窗口时,您将从控件中获取值并将其放入变量中.然后,您从调用上下文中读取变量.

The dialog and its controls is not created until you call DoModal() and as already pointed, is destroyed already by the time DoModal() returns. Because of that you cannot call GetDlgItem() neither before, nor after DoModal(). The solution to pass or retrieve data to a control, is to use a variable in the class. You can set it when you create the class instance, before the call to DoModal(). In OnInitDialog() you put in the control the value of the variable. Then, when the window is destroyed, you get the value from the control and put it into the variable. Then you read the variable from the calling context.

这样的事情(请注意,我是直接在浏览器中输入的,因此可能会出现错误):

Something like this (notice I typed it directly in the browser, so there might be errors):

class CMyDialog : CDialog
{
  CString m_value;
public:  
  CString GetValue() const {return m_value;}
  void SetValue(const CString& value) {m_value = value;}

  virtual BOOL OnInitDialog();
  virtual BOOL DestroyWindow( );
}

BOOL CMyDialog::OnInitDialog()
{
  CDialog::OnInitDialog();

  SetDlgItemText(IDC_EDIT1, m_value);

  return TRUE;
}

BOOL CMyDialog::DestroyWindow()
{
  GetDlgItemText(IDC_EDIT1, m_value);

  return CDialog::DestroyWindow();
}

然后您可以像这样使用它:

Then you can use it like this:

CMyDialog dlg;

dlg.SetValue("stackoverflow");

dlg.DoModal();

CString response = dlg.GetValue();

这篇关于关闭模态MFC对话框后获取编辑框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆