MFC - Edit Box


Advertisements

An Edit Box is a rectangular child window in which the user can enter text. It is represented by CEdit class.

Let us into a simple example by creating a new MFC dialog based project.

Step 1 − Remove the caption of Static Text control and drag one button and one Edit control.

Edit Box

Step 2 − Add a control variable m_editCtrl for edit control and value variable m_strTextCtrl for Static text control.

Step 3 − Add the event handler for button click event.

Edit Box

Step 4 − Here is the implementation of event handler for button click event.

void CMFCEditDlg::OnBnClickedButton1() {
   // TODO: Add your control notification handler code here
   CString str = _T("");
   m_editCtrl.GetWindowTextW(str);
   
   if (!str.IsEmpty())
      m_strTextCtrl = str;
   else
      m_strTextCtrl = _T("Write Something");
   UpdateData(FALSE);
}

Step 5 − When the above code is compiled and executed, you will see the following.

Edit Box

Step 6 − When you write text in the edit control and click Display, it will update that text on Static Text Control.

mfc_windows_controls.htm
Advertisements