4
votes

I created a control's variable for CEdit:

class CGateDlg : public CDialog
{
    ...
    public:
        // here is my control's variable
        CEdit m_edit_a;
        // here I map variable to control
        virtual void DoDataExchange(CDataExchange* pDX);
}

And this is how I map my variable to the control:

void CGateDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT_A, m_edit_a);
}

This is how it works: user types some text into the edit box. Then he presses the "Reset" button which clears the edit box. This is a piece of code responsible for clearing edit box after clicking Reset button:

void CGateDlg::OnBnClickedReset()
{
    // clear edit box 
    m_edit_a.SetWindowTextW(L"");
}

Application starts without any errors. I type some text into EditBox and hit "Reset" button. Then I get an error which leads me to winocc.cpp, line 245 (ENSURE(this)):

void CWnd::SetWindowText(LPCTSTR lpszString)
{
    ENSURE(this);
    ENSURE(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));

    if (m_pCtrlSite == NULL)
            ::SetWindowText(m_hWnd, lpszString);
    else
            m_pCtrlSite->SetWindowText(lpszString);
}

I think the problem is with the hWnd:

this    0x0030fa54 {CEdit hWnd=0x00000000}  CWnd * const

but how to fix it ?

Everything works fine when I access my control's value using this:

CEdit *m_edit_a;
m_edit_a = reinterpret_cast<CEdit *>(GetDlgItem(IDC_EDIT_A));
m_edit_a->SetWindowTextW(L"");

What am I doing wrong ?

4
could you post code for the whole class?Karoly Horvath
How do you create CGateDlg? If you aren't creating it from the resource id then DDX_Control may not be working for you and you may need to explicitly call m_edit_a.Create in a OnCreate overload for CGateDlg before you can use the CEditAJG85
What exactly do you mean by "creating it from the resource id" ? Could you please post me a link or a sample where I can see how it should look ? I'm at work at this moment but I'll try all your ideas when I get home and paste here whole headers and classes if necessary.Kamil N.

4 Answers

9
votes

I can see two possibilities:

  1. The control does not exist when the dialog starts. The first thing that CDialog::OnInitDialog will do is call DoDataExchange, so if you're creating the control later in the initialization process it's too late.

  2. Your own OnInitDialog is not calling CDialog::OnInitDialog so DoDataExchange is not being called.

1
votes

I think you should no use directly the meber of your control (in this case m_edit_a). Instead you should use a memeber variable, let's say CStrimg m_edit_data, and you should link it to the control:

DDX_Text(pDX, IDC_EDIT_A, m_edit_data); // as you did it in DDC_Cotrol

Now you can use directy the variable, but in order the control to be updated you should use the following code before using it:

UpdateData(true); // unlocks the control in a sense
m_edit_data = "this is my test";
UpdateData(false); // locks the control again (in a sense)

This is normal procedure in MFC :), hope I helped...

ohh... you should also add the control to String Table ... (let me know if you do not know)

0
votes

I can not find something wrong with you. I Create a new project using VC6.0,and associate a variable to the Edit,just link you do. the exe operates normally.

class CEditTestDlg : public CDialog
{
// Construction
public:
CEditTestDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CEditTestDlg)
enum { IDD = IDD_EDITTEST_DIALOG };
CEdit   m_Edit;
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CEditTestDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL

......

.cpp

void CEditTestDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CEditTestDlg)
    DDX_Control(pDX, IDC_EDIT1, m_Edit);
    //}}AFX_DATA_MAP
}
void CEditTestDlg::OnBnClickedReset() 
{
    // TODO: Add your control notification handler code here
    m_Edit.SetWindowText("tttt");
}

so,I think it is not a code problem.You had better try again.

0
votes

If your dialog starts off calling CDialog::OnInitDialog() and your DoDataExchange starts off calling CDialog::DoDataExchange but still you have null hWnd pointers and get CNotSupportedException, make sure your resource (rc) file's dialog template includes all the controls (IDC_) and such you have in DoDataExchange.

Check for overriding definitions if using a DLL that also provides resources.