MFC Edit control produces debug assert on first action

Bryan Kelly 486 Reputation points
2025-04-26T21:03:46.9+00:00

Windows 11, Visual Studio 17.13.3, C++, MFC, dialog app

Question:  In the dialog constructor,  add code for an Edit Control to clear it and get a popup dialog stating: Debug Assertion Failed!

Details

I solicited Bing.com and ChatGPT (right name?) for example code and followed it.  Found a learn.microosft.com web page and followed it.  They give the same advice.  Evidently there is something I don’t recognize because both produced the same result.

Create an MFC project with C++, add a second dialog, and get that dialog to open, build, run, be sure that works.  In the second dialog, drop an Edit control and add code in the constructor to clear it.  At the bottom of the h file VS automatically added this.

class C_Show_Numbers : public CDialog

{

            ``DECLARE_DYNAMIC(C_Show_Numbers)

public:

            ``CEdit ec_show_numbers_3;

            ``afx_msg void OnEnChangeShowNumbers3();

};

In the dot cpp file I added this

C_Show_Numbers::C_Show_Numbers(CWnd* pParent /*=nullptr*/)

            ``: CDialog(IDD_SHOW_NUMBERS, pParent)

{``           ``ec_show_numbers_3.Clear();

            ``ec_show_numbers_3.SetWindowTextW(_T("third text"));``  ``}

The result is the dialog shown below.

edit_contol_assert_01 So what did I miss in the instructions?

Developer technologies Visual Studio Debugging
{count} votes

1 answer

Sort by: Most helpful
  1. Bryan Kelly 486 Reputation points
    2025-05-05T01:50:22.9466667+00:00

    Visual Studio 2022, Version 17.13.3, just in case

    I discovered this quite by accident and am surprised that no one mentioned it.

    Create an MFC project in C++. I suspect this will work for other languages.

    Drag and edit control onto the dialog. Right click, Add Variable, name it, take the defaults.

    VS adds to the dialog class, the class created with Dlg postfixed to the project name.

    Go to the dialog class, scroll down to method OnInitDialog() and find the comment just above the line: return TRUE;

    Put the cursor at the end of the comment line and press enter. On my computer, within a few seconds, VS displayed this line of code:

    cv_edit_01.SetWindowTextW(_T("Hello World!"));

    It was displayed with a gray shadow text. Press tab to accept the prompt and insert the suggested code. Do it a few times for more suggestions.

    cv_edit_01 is the name of my variable. Build and run, the dialog has that text.

    Complications: My bing.com searches and other search results either would not show what I requested, or showed some lines of code that did not make sense. (To me anyway)

    I had done the search: microsoft class description for cedit class

    It provided much information, but not how to prepare it for use. After finding out how to get started with it, the Microsoft web page about the class is great.

    I suspect that many people already know this, but wonder why no one mentioned it.

    Oh well. Got it now.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.