943,520 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2817
  • C++ RSS
Mar 6th, 2009
0

CEdit capture Enter

Expand Post »
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
cpp Syntax (Toggle Plain Text)
  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
cpp Syntax (Toggle Plain Text)
  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:
cpp Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 18
Solved Threads: 0
Light Poster
GadiK is offline Offline
32 posts
since Dec 2008
Mar 6th, 2009
0

Re: CEdit capture Enter

Click to Expand / Collapse  Quote originally posted by GadiK ...
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
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 6th, 2009
0

Re: CEdit capture Enter

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.
Reputation Points: 18
Solved Threads: 0
Light Poster
GadiK is offline Offline
32 posts
since Dec 2008
Mar 6th, 2009
0

Re: CEdit capture Enter

Click to Expand / Collapse  Quote originally posted by GadiK ...
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
C++ Syntax (Toggle Plain Text)
  1. DDX_Control(pDX, IDC_EDIT1, m_eFirst);
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 6th, 2009
0

Re: CEdit capture Enter

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
Reputation Points: 18
Solved Threads: 0
Light Poster
GadiK is offline Offline
32 posts
since Dec 2008
Mar 10th, 2009
0

Re: CEdit capture Enter

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:
cpp Syntax (Toggle Plain Text)
  1. #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)
  1. ::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)
  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
Reputation Points: 18
Solved Threads: 0
Light Poster
GadiK is offline Offline
32 posts
since Dec 2008
Mar 10th, 2009
0

Re: CEdit capture Enter

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
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 13th, 2009
0

Re: CEdit capture Enter

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:

cpp Syntax (Toggle Plain Text)
  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
Reputation Points: 18
Solved Threads: 0
Light Poster
GadiK is offline Offline
32 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help creating a 2 dimensional array!
Next Thread in C++ Forum Timeline: Multiple student database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC