| | |
CEdit capture Enter
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 22
Reputation:
Solved Threads: 0
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
CMyEdit.cpp
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:
Thank you,
Gadi.
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
cpp Syntax (Toggle Plain Text)
#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 ); };
cpp Syntax (Toggle Plain Text)
#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:
cpp Syntax (Toggle Plain Text)
#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.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
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.
- 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
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
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.
Maybe you don't have the following in the dialog class implementation
C++ Syntax (Toggle Plain Text)
DDX_Control(pDX, IDC_EDIT1, m_eFirst);
•
•
Join Date: Dec 2008
Posts: 22
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2008
Posts: 22
Reputation:
Solved Threads: 0
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:
and in the OnChar() func (in CMyEdit) I'm sending a message like this:
in the Ctry_ENETRdlg.cpp I've entered this line to the message map:
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
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:
cpp Syntax (Toggle Plain Text)
#define UWM_ON_ENTER (WM_APP + 100)
and in the OnChar() func (in CMyEdit) I'm sending a message like this:
cpp Syntax (Toggle Plain Text)
::SendMessage(GetParent()->m_hWnd, UWM_ON_ENTER, 0, 0);
in the Ctry_ENETRdlg.cpp I've entered this line to the message map:
cpp Syntax (Toggle Plain Text)
. . . 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
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
The function prototype for
See User-Defined Handlers
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
•
•
Join Date: Dec 2008
Posts: 22
Reputation:
Solved Threads: 0
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:
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
cpp Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: help creating a 2 dimensional array!
- Next Thread: Multiple student database
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





