Eating returns in a single line edit | CodeGuru

Eating returns in a single line edit

I was quite frustrated in my app – I have a dialog with a few single-line edit controls; after updating the edits, when I pressed Enter the dialog was closed (IDOK was the default button). “Want return” option is unfortunately valid only for multiline edits. Here is my fix: I override PreTranslateMessage. If there is […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

I was quite frustrated in my app – I have a dialog with a few
single-line edit controls; after updating the edits, when I pressed
Enter the dialog was closed (IDOK was the default button).
“Want return” option is unfortunately valid only for multiline edits.

Here is my fix:

I override PreTranslateMessage. If there is WM_KEYDOWN with VK_ENTER
going to the edit, I set the focus to the default button and discard
the message. If the dialog has no defualt button, I let VK_ENTER
through – no problem this time.

I check if the window is an editbox by calling GetClassName and
comparing the return value to “Edit”. There are much more
possibilities – comparing the FromHandle(hwnd) to array of CWnd*, to
array of IDs (comparing to GetDlgItem() return values) etc.

For the code to be completely correct, it should check for the edit’s
windows style and it should not act if it is multi-line, want-return
edit. [I haven’t done that as I don’t use multiline edits.]

1. To your override of CDialog, add the following method:

	virtual BOOL PreTranslateMessage(MSG *);

2. Implement this function, for example:

BOOL CMyDialog::PreTranslateMessage(MSG *pMsg)
{
	if (pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
	{
		DWORD def_id=GetDefID();
		if (def_id!=0)
		{
			CWnd *wnd=FromHandle(pMsg->hwnd);
			// you may implement other ways of testing, e.g.
			//  comparing to array of CWnd*, comparing to array of IDs etc.
			char class_name[16];
			if (GetClassName(wnd->GetSafeHwnd(),class_name,sizeof(class_name))!=0)
			{
				if (strnicmp(class_name,"edit",5)==0)
				{
					GetDlgItem(LOWORD(def_id))->SetFocus();
					return TRUE;
					// discard the message!
				}
			}
		}
	}
	// be a good citizen - call the base class
	return CDialog::PreTranslateMessage(pMsg);
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.