Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Allowing a <RETURN> to do a NextDlgCtrl( )

Posted on 1998-03-03
11
Medium Priority
?
505 Views
Last Modified: 2011-10-03
I need to have my Windows 3.1 app (built using MSVC++ 1.52) allow the user to hit the <ENTER> key to jump to the next control in the TAB order on the dialog box.  I want to let the person use the <ENTER> key also.

I would like the code to do this.  I have tried -


void NewType::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      if (nChar == VK_RETURN)
      {
            NextDlgCtrl( );
      }            
      CDialog::OnChar(nChar, nRepCnt, nFlags);
}

int NewType::OnCharToItem(UINT nChar, CListBox* pListBox, UINT nIndex)
{
      if (nChar == VK_RETURN)
      {
            NextDlgCtrl( );
      }            
      return CDialog::OnCharToItem(nChar, pListBox, nIndex);
}

void NewType::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
                                                                   
But it never calls these member functions.  What am I missing?

Thanks
 
0
Comment
Question by:azar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 

Author Comment

by:azar
ID: 1182982
Edited text of question
0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1182983
Your dialog will never get these messages because the keyboard focus is on a specific child control such as an edit control.

One idea would be to create a default push button on your dialog that is hidden. Then when the user presses the enter key, the push button is pressed. Use a handler in your dialog to catch this and use a

  SendMessage(GetSafeHwnd(), WM_NEXTDLGCTL, 0, FALSE);

to simulate a tab being pressed.


0
 

Author Comment

by:azar
ID: 1182984
The focus isn't the issue.    I am setting the focus to the first object and want the <ENTER> key to move you to the next one.  I'd be happy to have the main dialog box window grab the keyboard input  and then check for which  control has the focus if necessary

If anyone has an answer, please post the few lines necessary to find out that the user hit the Enter key and then do the NextDlgItem command to move to the next control.

thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:nietod
ID: 1182985
The focus IS the issue.  The dialog item has the focus.  So the dialog item, which is a window, is getting the WM_CHAR and WM_KEYDOWN messages.  You are looking for those messages in the dialog window's procedure.  The dialog window will not get the messages because it doesn't have te focus.

One way around this would be to sub-class the controls so that you can look for the enter keystroke in each of the controls' window procedures.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182986
By "dialog item" I mean a control on the dialog window.  Sorry about the poot terminology.
0
 

Author Comment

by:azar
ID: 1182987
What about just passing the key entry message up to the parent, the dialog box, and have it handle the entry.  

I definately don't want to create 50 'false buttons' to allow every dialog control just to let the user move around using the ENTER key rather than the TAB.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182988
The only way to pass the  key message up to the parent is by sub-classing the control.  If you are going to sub-class the control, you might as well just handle it in the control's window procedure.  

The false buttons Thui proposed would be simpler, but kinda cludgey.  It might cause som suprises later.  
0
 
LVL 2

Accepted Solution

by:
Srw earned 200 total points
ID: 1182989
Here's a little trick I found about the way that MFC dialogs work.  If you override the OnOK() member of a dialog box, it should pretty much always be called whenever you hit the enter key while focus is anywhere in your dialog.  The same applies for the escape key and OnCancel().

Great news...  Calling GetFocus() will give the CWnd* to the control that had focus before the key was pressed -- The keystroke will not change the focus!

More Great news...  This works in both 16 and 32 bit VC++ !!!

For example:

void CMyDialog::OnOK()   // override from CDialog
{
    CWnd* pFocus = GetFocus();   // get the control with focus when return was hit

    if (pFocus != GetDlgItem(IDC_DoneButton)) // If you don't want to exit...
    {
        NextDlgCtrl();   // just move the focus
        return;
    }
    else
    {
        CDialog::OnOK();    // closes dialog box and returns IDOK from
                                     // CMyDialog::DoModal()
        return;
    }
}


Unfortunately, if you want to trap any keys other than return and escape, you'll have to subclass every window in the dialog box and trap the keydown, etc. messages.  I've tried it, and it'll work, but it's a huge pain!!!
0
 

Author Comment

by:azar
ID: 1182990
That nailed it!  I had already overridden the OnOK and I had done pretty much what you said, I had the code a bit/lot more convoluted, so the focus wan't getting snagged for a few cases.

This is simple, elegant, and exactly what I was asking for.   I 'KNEW' it could be done easily  (I had been shown this by someone a long time ago) but forgot how, and with people saying there wasn't a simple way, I kept trying to remember since I knew there was.

THANK YOU VERY MUCH.   The key here was that the users wanted to use the <ENTER> key and not the <TAB> so I wanted to get that working for them.
0
 
LVL 2

Expert Comment

by:Srw
ID: 1182991
That is EXACTLY the same reason I needed.  I was writing a windows program to replace an old dos one.  The users were very computer illiterate, and it *really* needed to use the same keystrokes.

I also use this method when I don't want the return/escape keys to do anything at all.
0
 

Author Comment

by:azar
ID: 1182992
Same here.  The folks are completely computer illiterate and the idea of hitting <TAB> was something that was terribly confusing to them.  This will makes things much easier for them.  Thanks again.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

722 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