0

I have the following code in trying to set the text in a Cedit text box:

class CMetaDlg : public CDialogEx
{
public:
CMetaDlg();

// Dialog Data
enum { IDD = IDD_META };

protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
DECLARE_MESSAGE_MAP()
public:
CEdit m_author;
CEdit m_sources;
afx_msg void OnBnClickedOk();
};

CMetaDlg::CMetaDlg() : CDialogEx(CMetaDlg::IDD)
{
}

void CMetaDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_AUTHOR, m_author);
DDX_Control(pDX, IDC_SOURCES, m_sources);
}

BEGIN_MESSAGE_MAP(CMetaDlg, CDialogEx)
ON_BN_CLICKED(IDOK, &CMetaDlg::OnBnClickedOk)
END_MESSAGE_MAP()

void CEmergenceApp::OnFileMeta()
{


CMetaDlg md;    
md.DoModal();
md.m_author.SetWindowTextW(CEmergenceView::GetDoc()->author);
md.m_sources.SetWindowTextW(CEmergenceView::GetDoc()->sources);
}

This gives me a debug assertion error. I am assuming the problem lies in the lines:

md.m_author.SetWindowTextW(CEmergenceView::GetDoc()->author);
md.m_sources.SetWindowTextW(CEmergenceView::GetDoc()->sources);

As commenting them out, everything works fine.

1 Answers1

0

When you call DoModal the dialog is created, the edit controls are created and then when the user clicks OK or Cancel the edit controls and dialog window are destroyed. Then DoModal returns. I do not understand what you are trying to do by trying to set the edit control's text after the dialog box closes.

ScottMcP-MVP
  • 10,036
  • 2
  • 13
  • 15
  • OnInitDialog is the only place it can work. The edit controls do not exist before DoModal and they do not exist after DoModal. What you must do is set dialog CString member variables before DoModal. Then in OnInitDialog set the controls from the CString member variables. You are having a lot of trouble with very basic MFC operations because you have apparently not used a book or tutorial to learn how things work. A little studying would go a long way. – ScottMcP-MVP Mar 24 '14 at 13:59