Link to home
Start Free TrialLog in
Avatar of baraka
baraka

asked on

Help in trapping <Enter> event from edit box


I am using VC++ (16 bit ver.) MFC for creating a window that contains controls. One of these (sum of two) controls is a simple single-line edit box for user input.
My problem is: The default EN_ messages do not react to <Enter>, whilst I want to update changes(process input) when the user presses the <Enter> key. Does anyone know how I can solve this ??

Just to clarify a bit,I am not asking what is the raw code of <Enter> but what can replace OnChar inside my CView (or any other program element that qualifies for this purpose) in order to use that raw-code or virtual key or whatever. What I am asking is WHERE,if at all, can I catch the <Enter> event when sending it while being inside an edit control ( Which BTW is part of a dialog with IDR_MAINFARAME id, i.e. the main and only view )
Its possible that I should add some more details:
 - My application is an SDI one.
 - A dialog is attached through a template to the    framewindow and view.
- This dialog hasn't got buttons (e.g OK button)
- This dialog has an edit control, and I need to know where   is MFC directing an event when pushing <Enter> from   inside this control.

  Thanks, Barak
ASKER CERTIFIED SOLUTION
Avatar of mbhakta
mbhakta

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of baraka
baraka

ASKER

Sorry, but I have already tried that and it doesn't work.
What it does is BEEPs when in the last line or get to the next one. (i.e. the default behaviour for this control)

My code is:

void View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{  

        // The following is never reached

      AfxMessageBox("Inside OnChar function");

         CMyAppDoc* pDoc =                                               (CMyAppDoc*)GetDocument();
         ASSERT_VALID(pDoc);

      switch(nChar)
      {
            case VK_RETURN:
            {
                    UpdateData();
                  AfxMessageBox(m_input_string);

                        // m_input_string is the edit DDX
                        // data member  
                  
                  break;
            }
            case VK_ESCAPE:
            {
                    UpdateData();
                  AfxMessageBox("ESC");

                        break;
            }        
            //default:
      }
      
      CFormView::OnChar(nChar, nRepCnt, nFlags);      
}

Just in case, I put the same code for OnKeyDown. Both were created using the class wizard. (So I didn't miss a message map)

The problem is, I think, that what I need works only for CEdit, so maybe if I derive my own class from a CEdit class that will override the OnChar message and then start changing IDs in the DDXes and  maybe things inside "resource.h" too, it might work.

What do you think? tnks, Barak.
That's what I meant. Derive a class from CEdit.... Also avoid the 'UpdateData()' stuff inside system critical messages. This will only slow down your system. Instead use direct calls like GetWindowText() instead.
Avatar of baraka

ASKER

Tried it. Didn't work...I think I am probably missing something.
I have created a MyEdit class with a constructor that calls the CEdit one and then use Create(...) ( Using the ID of the edit-box that was created by the resource editor + the correct styles ).to create the edit-box.
I mapped the OnChar inside my new class to respond to <Enter>.
Then, Inside my CFormView derived class I added a data member
of MyEditPtr kind and in the Ctor constructed it.(With new)
But, running with dubugger, I saw that my program doesn't even enter the MyEdit::OnChar no matter what.
Should I first remove the Edit-box that's created by my resource editor in the first place? (Because of possible override?)

When the WANTRETURN|MULTILINE styles are on, pressing <Enter>
(At least before adding my derived edit...) causes a BEEP.
If I would only know what element in the program creates that beep, i.e. who traps the <Enter>...
Avatar of baraka

ASKER

Well, it seems that afterall the edit box can trap the <Enter> key, but in its OnUpdate/OnUpdateChange (not in OnChar) and when MULTILINE|WANTRETURN are on. I can trap it by seeing no difference in the input-string between the before <Enter> event
and the after <Enter> event. How to cancel the BEEP is another matter for considering.