CEdit capture Enter

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 24
Reputation: GadiK is an unknown quantity at this point 
Solved Threads: 0
GadiK GadiK is offline Offline
Newbie Poster

CEdit capture Enter

 
0
  #1
Mar 6th, 2009
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
  1. #include "afxwin.h"
  2.  
  3. class CMyEdit : public CEdit
  4. {
  5. DECLARE_DYNAMIC(CMyEdit);
  6. protected:
  7. DECLARE_MESSAGE_MAP()
  8. public:
  9. afx_msg UINT OnGetDlgCode();
  10. afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
  11. afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
  12. BOOL PreTranslateMessage( MSG* pMsg );
  13. };
CMyEdit.cpp
  1. #include "stdafx.h"
  2. #include "CMyEdit.h"
  3.  
  4. IMPLEMENT_DYNAMIC(CMyEdit, CEdit)
  5.  
  6. BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
  7. ON_WM_GETDLGCODE()
  8. ON_WM_CHAR()
  9. ON_WM_KEYDOWN()
  10. END_MESSAGE_MAP()
  11.  
  12. UINT CMyEdit::OnGetDlgCode()
  13. {
  14. // Assure we will receive Enter key
  15. return CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS;
  16. }
  17.  
  18. void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  19. {
  20. int i=0;
  21.  
  22. i++;
  23.  
  24. if (nChar == '\r')
  25. AfxMessageBox(_T("Got Enter"));
  26.  
  27. CEdit::OnChar(nChar, nRepCnt, nFlags);
  28. }
  29.  
  30. void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  31. {
  32. int i=0;
  33.  
  34. i++;
  35.  
  36. if (nChar == VK_RETURN)
  37. AfxMessageBox(_T("Got Enter"));
  38.  
  39. CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  40. }
  41.  
  42. BOOL CMyEdit::PreTranslateMessage(MSG *pMsg)
  43. {
  44. if (pMsg->message == WM_KEYDOWN)
  45. {
  46. if (pMsg->wParam == VK_RETURN)
  47. AfxMessageBox(_T("Got Enter"));
  48. }
  49. return CEdit::PreTranslateMessage(pMsg);
  50. }

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:
  1. #pragma once
  2. #include "afxwin.h"
  3. #include "CMyEdit.h"
  4.  
  5.  
  6. // Ctry_ENTERDlg dialog
  7. class Ctry_ENTERDlg : public CDialog
  8. {
  9. // Construction
  10. public:
  11. Ctry_ENTERDlg(CWnd* pParent = NULL); // standard constructor
  12.  
  13. // Dialog Data
  14. enum { IDD = IDD_TRY_ENTER_DIALOG };
  15.  
  16. protected:
  17. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  18. afx_msg void OnOK();
  19.  
  20. // Implementation
  21. protected:
  22. HICON m_hIcon;
  23.  
  24. // Generated message map functions
  25. virtual BOOL OnInitDialog();
  26. afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
  27. afx_msg void OnPaint();
  28. afx_msg HCURSOR OnQueryDragIcon();
  29. DECLARE_MESSAGE_MAP()
  30. private:
  31. CMyEdit m_eFirst;
  32. CEdit m_eSecond;
  33. CButton m_bFirst;
  34. CButton m_bSecond;
  35. };

Thank you,
Gadi.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: CEdit capture Enter

 
0
  #2
Mar 6th, 2009
Originally Posted by GadiK View Post
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 24
Reputation: GadiK is an unknown quantity at this point 
Solved Threads: 0
GadiK GadiK is offline Offline
Newbie Poster

Re: CEdit capture Enter

 
0
  #3
Mar 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: CEdit capture Enter

 
0
  #4
Mar 6th, 2009
Originally Posted by GadiK View Post
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
  1. DDX_Control(pDX, IDC_EDIT1, m_eFirst);
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 24
Reputation: GadiK is an unknown quantity at this point 
Solved Threads: 0
GadiK GadiK is offline Offline
Newbie Poster

Re: CEdit capture Enter

 
0
  #5
Mar 6th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 24
Reputation: GadiK is an unknown quantity at this point 
Solved Threads: 0
GadiK GadiK is offline Offline
Newbie Poster

Re: CEdit capture Enter

 
0
  #6
Mar 10th, 2009
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:
  1. #define UWM_ON_ENTER (WM_APP + 100)

and in the OnChar() func (in CMyEdit) I'm sending a message like this:
  1. ::SendMessage(GetParent()->m_hWnd, UWM_ON_ENTER, 0, 0);

in the Ctry_ENETRdlg.cpp I've entered this line to the message map:
  1. .
  2. .
  3. .
  4. ON_MESSAGE(UWM_ON_ENTER, &Ctry_ENTERDlg::OnBnClickedButton1)
  5. .
  6. .
  7. .

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: CEdit capture Enter

 
0
  #7
Mar 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 24
Reputation: GadiK is an unknown quantity at this point 
Solved Threads: 0
GadiK GadiK is offline Offline
Newbie Poster

Re: CEdit capture Enter

 
0
  #8
Mar 13th, 2009
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:

  1. 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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC