C ++ / MFC Error accessing control variable

I created a control variable for CEdit:

class CGateDlg : public CDialog { ... public: // here is my control 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); } 

Here's how it works: the user enters text in the edit box. Then he presses the "Reset" button, which clears the editing window. This is part of the code responsible for clearing the edit window after clicking the Reset button:

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

The application starts without errors. I type in the EditBox and click on the "Reset" button. Then I get an error message that 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 hWnd:

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

but how to fix it?

Everything works fine when I access my control value with 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
source share
4 answers

I see two possibilities:

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

  • Your own OnInitDialog does not call CDialog :: OnInitDialog, so DoDataExchange is not called.

+8
source

I think that you should not directly use the measure of your control (in this case m_edit_a ). Instead, you should use the memeber variable, say CStrimg m_edit_data , and you should bind it to the control:

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

Now you can use the variable directive, but in order for the control to be updated, you must 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 a normal procedure in MFC :), I hope I helped ...

ohh ... you should also add the control to the String Table ... (let me know if you don't know)

+1
source

I can’t find something wrong with you. I am creating a new project using VC6.0 and will bind the variable to the editing, just a link. exe is working fine.

 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 this is not a problem with the code. You better try again.

0
source

If your dialog starts with a call to CDialog :: OnInitDialog (), and your DoDataExchange starts to call CDialog :: DoDataExchange, but you have null hWnd pointers and you get a CNotSupportedException, make sure all the controls are in the resource file (rc) dialog box (IDC_) and the ones you have in DoDataExchange.

Check redefinition of definitions when using DLL which also provides resources.

0
source

Source: https://habr.com/ru/post/1391546/


All Articles