Solved

Help in trapping <Enter> event from edit box

Posted on 1997-06-16
5
306 Views
Last Modified: 2013-11-20

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
0
Comment
Question by:baraka
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
mbhakta earned 70 total points
ID: 1302127
First you need to make sure the style of your edit control has the following attributes :
- It it multiline ES_MULTILINE
- It has 'want enter' enabled ES_WANTRETURN .
Now override your OnChar of the edit control. The code would look somewhat like this

CMyEdit::OnChar(int nChar , ...)
{
    if ( nChar == 13) //enter pressed
    {
       //do it
    }
    CEdit::OnChar(...);

}

This will work for dialogs views  and property pages without any trouble.

0
 

Author Comment

by:baraka
ID: 1302128
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.
0
 
LVL 4

Expert Comment

by:mbhakta
ID: 1302129
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.
0
 

Author Comment

by:baraka
ID: 1302130
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>...
0
 

Author Comment

by:baraka
ID: 1302131
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.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sum13 challenge 24 90
post4 challenge 28 93
JQuery serialize and unserialize 8 51
"Black Box" Testing of Control System Software 2 25
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now