Solved

Help in trapping <Enter> event from edit box

Posted on 1997-06-16
5
302 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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