The CEdit class provides the functionality of an edit control in Microsoft Windows based systems. It is a rectangular child window which can be used to enter text in GUI Programs. This article explains how to use the CEdit class for some of the important operations to be performed in a dialog based program. This is also applicable in SDI/MDI applications which use a dialog box.

CEdit – Setting up the program:

The following steps explain how to setup the base application/dialog box for using the edit control. The class CEdit will be used to manipulate the edit control being used. If this basic set up is already done, readers can skip this section and move to the next one.

  • Create a new MFC AppWizard executable project as a dialog based application.
  • The application can also be an SDI/MDI app, where in the dialog can be inserted as a resource into the SDI/MDI projects.
  • From the controls toolbox, choose the edit control and place it on a dialog box. In the properties dialog of the edit control, enter the resource ID as IDC_EDTEST.
  • If the edit control is supposed to accept only numbers, in the Edit control –> Properties –> Styles tab, set the number property to true by checking the checkbox provided. This makes the CEdit based edit control accept only numbers.
  • Add a class member for the CEdit for the edit control. The code sample in this CEdit article assumes the variable name to be m_Edit.

CEdit – Setting and Retrieving values:

The member functions of CEdit, CEdit .SetWindowText() and CEdit .GetWindowText() can be used to set and get the values on the Edit control. The following is the code sample for setting the values in edit controls.

     //Set the value on CEdit
     CString l_strValue = "Test Value to be set on CEdit";

     //Get the value from the CEdit control
     m_Edit.GetWindowText(l_strValue); //This gets the value into the CString variable l_strValue

CEdit – Using DDX mechanism:

Dynamic Data Exchange offered by MFC is a very useful feature, which allows programmers to ignore the control variables and stick to value based variables. This is an easy way to initialize the values of controls in a dialog box and to retrieve values from the controls. To enable the CEdit control for DDX mechanism the following steps as outlined here may be performed.

Open the Class Wizard.
In the Member Variables tab, click Add Variable button.
In the Add Member Variable dialog box, choose the Category as Value and choose the Variable type as a suitable one. If the values can be manipulated are in character arrays, choose CString, if numbers are going to be manipulated choose Number etc.,
Click Ok and save the project. The control is now DDX enabled.
The following code sample may be used for setting and getting the value on a DDX enabled CEdit control. The sample assumes the variable name as m_strEdit.

//To set the value on a DDX based CEdit control
void DlgName::SetvalueonEdit()

m_strEdit = "Test Value set for DDX";

//To get the value on a DDX based CEdit control
void DlgName::GetvalueFromEdit()

CString strStoreValue;
strStoreValue = m_strEdit ;


Note: The CEdit control can also be set on a multi-line mode. While working on a multi-line mode the CEdit will give some problems for using the function SetWindowText. The end of line characters won’t be shown properly. To solve this problem, for every end of line append the string with a “rn”.