Home

Edit Boxes

 

Overview

An edit box is a control used to either display text, to request text, or to do both. It it provided as a rectangular control with a sunken white background and 3-D borders.

To add an edit box and click the desired area on a form or a dialog box. To add a new edit box programmatically, declare a variable or a pointer to CEdit using its constructor. To initialize it, call its Create() method. Here is an example:

BOOL CLabelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CEdit *Musician = new CEdit;

	Musician->Create(WS_CHILD | WS_VISIBLE,
		             CRect(10, 50, 150, 70), this, 0x1552);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

To change the text of the control, a user can click it and start typing. This is possible if allowed it, which is the default behavior. If you don't want the user to be able to change the text of an edit box, at design time, check its Read Only check box or set it to True. To dynamically set an edit box to read-only, add the ES_READONLY style to it.

The text of an edit box displays its characters at they are set in the alphabet. If you want to hide the characters of an edit box, check its Password check box at design time. In this case, the characters would appear as x. If you prefer a different character, call the SendMessage() function with the EM_SETPASSWORDCHAR as the message. Here is an example:

BOOL CLabelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_AccountPassword.SendMessage(EM_SETPASSWORDCHAR, '&', 0);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

Multiline Edit Boxes

By default, an edit box is used to display a single line of text. An edit box is referred to as multiline when it can display its text on more than one line.

To create a multiline edit box, use the same Edit control as the above single line Edit box. To make this a multiline edit box, set its Multiline property to True. To do this at run time, add the ES_MULTILINE style to the CEdit. At design time, to make it obvious that the control can display more than one line of text, heighten it:

Edit Box

If the user is typing text in an edit control and press Enter, the control, usually a button, that is the default would be activated. This feature is valuable for a single line edit box. If you are creating a multiline edit box, you should allow the user to press Enter while entering text into the control. This would prevent the default button from being activated and would transfer the caret to the next line. To provide this functionality, add the ES_WANTRETURN style or, at design time, set the Want Return property to True.

If the text of the edit control is longer than the edit control can display at one time, you should equip it with scroll bars. To do this, at design time, set the Horizontal and/or the Vertical Scroll properties to True. At run time, add the WS_HSCROLL and/or the WS_VSCROLL properties.

Edit Box Methods

The edit box of Visual C++ does not allow you to set its text at design time but this can easily be done at run time. You can programmatically change the text of an edit box control. If you had Added a Value Control to the edit box and you had set the type to CString, to specify its text, call the CString::Format() method and make sure you update its value with the dialog box by calling UpdateData(FALSE). Here is an example:

BOOL CLabelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_Display.Format("On s'en fout");

	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

If you had added a Control Variable of type CEdit, Visual C++ would initialize it with an empty string in the constructor. You then use the constructor to initialize the control. Here is an example:

CLabelDlg::CLabelDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLabelDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLabelDlg)
	m_Display = _T("Kassav' - Nou La");
	//}}AFX_DATA_INIT
}

At any time, you can call the CWnd::SetWindowText() method to change the text of the edit box. If you did not add the control variable, you can get its pointer using CWnd::GetDlgItem(). Here is an example:

BOOL CLabelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CEdit *Display;

	Display = reinterpret_cast<CEdit *>(GetDlgItem(IDC_DISPLAY));
	Display->SetWindowText("Word Processing");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

Like the static text control, the edit box starts displaying its text from the left side of the allocated rectangle. This alignment is controlled by the Align Text property. At design time, you can set it to Left, Center, or Right. If you are programmatically creating the control, you can change its text alignment with the following styles: ES_LEFT, ES_CENTER, or ES_RIGHT respectively.

Edit Control Messages

To manage its role on a form or dialog box, an edit control is equipped with some messages known as notifications. These notifications messages are:

ON_EN_SETFOCUS: The event name for this notification is called OnSetFocus. It is sent when an edit box receives focus. This happens when the user clicks the edit box or after previously pressing Tab, to give focus to the control:

BEGIN_MESSAGE_MAP(CDialog3aDlg, CDialog)
	//{{AFX_MSG_MAP(CDialog3aDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_SETFOCUS(IDC_LAST_NAME, OnSetFocusLastName)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

...

void CDialog3aDlg::OnSetFocusLastName() 
{
	// TODO: Add your control notification handler code here
	m_Message.SetWindowText("The First Name has focus");
}

ON_EN_CHANGE: The OnChange event of this notification occurs as the user is typing text in the edit control. This happens as the user is changing the content of an edit control, which sends a message that the content of the edit box has changed. You can use this event to check, live, what the user is doing in the edit box. For example, if you create a dialog box or a form with a first and last names edit boxes, you can use another edit box to display the full name. You can implement the OnChange events of the edit boxes as follows:

BEGIN_MESSAGE_MAP(CDialog3aDlg, CDialog)
	//{{AFX_MSG_MAP(CDialog3aDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_SETFOCUS(IDC_LAST_NAME, OnSetFocusLastName)
	ON_EN_CHANGE(IDC_FIRST_NAME, OnChangeFirstName)
	ON_EN_CHANGE(IDC_LAST_NAME, OnChangeLastName)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

...

void CDialog3aDlg::OnChangeFirstName() 
{
	// TODO: Add your control notification handler code here
	m_FirstName.GetWindowText(m_strFirstName);
	m_LastName.GetWindowText(m_strLastName);
	CString FullName  = m_strFirstName + " " + m_strLastName;

	m_FullName.SetWindowText(FullName);
}

void CDialog3aDlg::OnChangeLastName() 
{
	// TODO: Add your control notification handler code here
	m_FirstName.GetWindowText(m_strFirstName);
	m_LastName.GetWindowText(m_strLastName);
	CString FullName  = m_strFirstName + " " + m_strLastName;

	m_FullName.SetWindowText(FullName);
}

ON_EN_UPDATE: The OnUpdate event is sent after the content of an edit box has changed but before the text is formally displayed.

ON_EN_MAXTTEXT: When adding a variable for the edit control, you can specify the maximum allowable number of characters the user can enter. If the user attempts exceeding this maximum, an OnMaxtext() event fires. You can catch this event to let the user know about the problem. If you set up the Maximum Characters fine, you do not need to perform any "if" or "while" checking. The edit control would do it itself.

ON_EN_ERRSPACE: The OnErrSpace event is event is sent if the compiler encountered a problem when attempting to allocate memory space to the control.

ON_EN_KILLFOCUS: The OnExit event occurs when the control loses focus. In the case of an edit box, this could happen if the control has focus and the user presses Tab; the edit box would lose focus.

Related Articles:

Net Price
Danillo Pizza
Strings in Dialog Box Controls
Dialog Data Transfer

Copyright 2003-2005 FunctionX, Inc.