Hello dear forum friends.
I know that this subject has been dealt with before. I read the threads about this issue in this forum however I still couldn't make it work. So I'm bringing this up again and hopefully you'll find it in your hearts to forgive and help me. :)

I've made a simple dialog (MFC) which contains 2 edit boxes 2 buttons which I've made and the 2 automated buttons OK and Cancel.
What I would like is that when write text in the first edit box and hit ENTER the dialog would push the first button that I created.

I understand that in order to capture the ENTER with the edit box control I need to make a derived class of CEdit and there process the messages. Here's my code:
CMyEdit.h

#include "afxwin.h"

class CMyEdit : public CEdit
{
	DECLARE_DYNAMIC(CMyEdit);
protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg UINT OnGetDlgCode();
	afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	BOOL PreTranslateMessage( MSG* pMsg );
};

CMyEdit.cpp

#include "stdafx.h"
#include "CMyEdit.h"

IMPLEMENT_DYNAMIC(CMyEdit, CEdit)

BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
	ON_WM_GETDLGCODE()
	ON_WM_CHAR()
	ON_WM_KEYDOWN()
END_MESSAGE_MAP()

UINT CMyEdit::OnGetDlgCode()
{
	// Assure we will receive Enter key
	return CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS;
}

void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	int i=0;

	i++;

	if (nChar == '\r')
		AfxMessageBox(_T("Got Enter"));
	
	CEdit::OnChar(nChar, nRepCnt, nFlags);
}

void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	int i=0;

	i++;

	if (nChar == VK_RETURN)
		AfxMessageBox(_T("Got Enter"));
	
	CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}

BOOL CMyEdit::PreTranslateMessage(MSG *pMsg)
{
	if (pMsg->message == WM_KEYDOWN)
	{
		if (pMsg->wParam == VK_RETURN)
			AfxMessageBox(_T("Got Enter"));
	}
	return CEdit::PreTranslateMessage(pMsg);
}

For simplicity I'll settle for showing a message when ENTER is pressed.
As you can see I've tried to capture both OnChar and KeyDown messages. I've also tried the PreTranslate() function.
But it doesn't work!!
Is there something in the main dialog code that I'm forgetting?

main header:

#pragma once
#include "afxwin.h"
#include "CMyEdit.h"


// Ctry_ENTERDlg dialog
class Ctry_ENTERDlg : public CDialog
{
// Construction
public:
	Ctry_ENTERDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_TRY_ENTER_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	afx_msg void OnOK();

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
private:
	CMyEdit m_eFirst;
	CEdit m_eSecond;
	CButton m_bFirst;
	CButton m_bSecond;
};

Thank you,
Gadi.

Recommended Answers

All 7 Replies

What I would like is that when write text in the first edit box and hit ENTER the dialog would push the first button that I created.

That functionality can be achieved by simply making sure that:

  • the first button (only) has the 'default button' -option checked
  • the first edit box does not have the 'want return' -option checked OR it is not a multiline edit control

Setting the Default to a specific button is not the solution I want because in the future I would want 10 edit boxes with 10 buttons to correspond to each box.
I'm looking for a more generic solution.
Thanks.

Setting the Default to a specific button is not the solution I want because in the future I would want 10 edit boxes with 10 buttons to correspond to each box.
I'm looking for a more generic solution.
Thanks.

Seems that I misunderstood the problem. Anyhow, the CMyEdit class' code that you have should capture the Enter key.
Maybe you don't have the following in the dialog class implementation

DDX_Control(pDX, IDC_EDIT1, m_eFirst);

Oh My God!!!!
I can't believe it, when I changed the type of the controller to the EditBox from CEdit to CMyEdit I forgot to change it in the DoDataExchange function :$ .
Luckily mitrmkar was there to save the day :)

Now all I have to do in the CMyEdit controller is to send a message to the parent Dialog where it'll be processed to activate the OnButton function. Hopefully that part will go smoothly.

Thank a lot

Well the sending message part did not go smoothly.
Reminder:
I have the main GUI class: Ctry_ENTERdlg
and a dervied class for capturing an ENTER in an Edit box: CMyEdit.

Now what I am trying to do is send a message from the CMyEdit controller to the main GUI (when ENTER is pressed).

I've defined a user message in CMyEdit.h:

#define UWM_ON_ENTER (WM_APP + 100)

and in the OnChar() func (in CMyEdit) I'm sending a message like this:

::SendMessage(GetParent()->m_hWnd, UWM_ON_ENTER, 0, 0);

in the Ctry_ENETRdlg.cpp I've entered this line to the message map:

.
.
.
ON_MESSAGE(UWM_ON_ENTER, &Ctry_ENTERDlg::OnBnClickedButton1)
.
.
.

This doesn't even compile!!
Could anyone please point out my mistakes and maybe post a link to a web page the explains how to do this sort of thing properly?

Thank you very very much,
Gadi

The function prototype for ON_MESSAGE() is afx_msg LRESULT memberFxn( WPARAM, LPARAM ); Ctry_ENTERDlg::OnBnClickedButton1 does not match, so you might add a new member function which handles the message.

See User-Defined Handlers

Yes that indeed was the problem. (It didn't compile because I used ";" at the end of a #define line). Now everything works. I send the message like this:

GetParent()->SendMessage(UWM_ON_ENTER, (WPARAM) GetDlgCtrlID());

And in the Ctry_ENTER cpp file I have a function that compares the wParam argument to existing resources in my dialog, and according to that activates the appropriate function.

Mitrmkar thanks a lot for your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.