Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 16: Text-Based Controls Visual C++ and MFC Fundamentals



  1. Return to MSVC


16.2.2..Edit Control Characteristics...............................................................


An edit box is a control based on the CEdit class. Therefore, to programmatically create
an edit box, declare a variable of CEdit type using its (default) constructor. To define the
characteristics of this control, you can call the CEdit::Create() method. Its syntax is:

BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);

The content of an edit box, that is, its value, is a string. It could be considered as null-
terminated constant (LPCTSTR) or a CString value.

Like every other control added during design, you should give some attention to the
identifier of an edit box. The first edit box placed on a form or dialog box receives an
identifier of IDC_EDIT1. The second would be identified as IDC_EDIT2, etc. It is highly
recommended that you give a meaningful and friendly identifier to each control. For
example, if an edit box is used to receive or display an address, you can set its identifier
to IDC_ADDRESS. If an edit box is used to enter an email address, you can change its
ID to IDC_EMAILADDRESS or IDC_EMAIL_ADDRESS. An identifier should have a
maximum of 30 characters. Notice that the ID contains C, which stands for Control.

If you plan to access an edit box in your code, you should create a variable for it. The
variable can be a CEdit or a CString object. The difference is that, if you want to access
the control as an MFC class, you should create the variable as a CEdit object. If you are
more interested with the value (as a string) of the control, you should declare the variable
as CString. Fortunately, you can add two variables for the same control.

Probably the most important characteristic of an edit control for both the programmer and
the user is the text it is displaying or that it can display. When you add an Edit control,
Visual C++ 6 displays Edit and Visual C++ 7 displays Sample edit box. This text is not
part of the control and does not show when the control comes up. It is only an indicator.
Free download pdf