Solved

Trapping <return> in dialog

Posted on 1998-08-09
7
271 Views
Last Modified: 2010-04-10
I have no OK button or items in my dialog which call the OnOK() function.  However, any time return is hit my program terminates.

I have tried PreTranslateMessage to no avail (perhaps I am calling it in the wrong place).  How can I trap the return?

I also have a single line CEdit control, will I need to trap <Return> entered in it separately?  Thanks!

P.S. the daily points haven't been updating, sorry I can't spend much more on this!
0
Comment
Question by:porear
7 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1169798
Specify the ES_WANTRETURN style for the edit control.  

Let me know if you have questions.
0
 
LVL 5

Author Comment

by:porear
ID: 1169799
From what I understand ES_WANTRETURN only applies to multiline edit boxes... this is a single line.  The main problem is the main dialog window, not just the edit box.  Thanks
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169800
I'm not sure this is a good way, but one way is to handle the WM_GETDLGCODE message and return DLGC_WANTALLKEYS.  This prevents the dialog window from interpreting the enter key.  However it prevents it form interpreting other keys as well.  Thus you would have to renable those behaviours that you wanted by looking for their keys and mimicing these standard behavior.   There may be an easier way.  I don't use the built-in dialogs, so I'm not sure.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

by:porear
ID: 1169801
I did try that too, but apparently there is still something wrong with my method.  Per something I found on a newsgroup:

In MyDlg.CPP:

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)

              ON_WM_GETDLGCODE()  // I added these two to the existing list
              ON_WM_KEYDOWN()

END_MESSAGE_MAP()

UINT CMyDlg::OnGetDlgCode()
{
              return DLGC_WANTALLKEYS | DLGC_HASSETSEL;
}

void CMyDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
              if (nChar == VK_RETURN)
              {      
                         MessageBox("Return hit");     //  Trapped the return, notify
              }
}

Still no luck...  Thanks for the input though, I know here has to be a simple solution to this, its got to be a common issue...
0
 
LVL 2

Expert Comment

by:warmcat
ID: 1169802
This is just a shot in the dark, but I understood that Enter selects the 'default button', defaultness being a button property you can set using Developer Studio in the normal way.  Why don't you try sticking a dummy button with this default property, perhaps with a dummy do-nothing OnClick() handler.  If that solves your problem, keep the button and use MoveWindow() to shift the button out of the visible dialog box area inside OnInitDialog().

Good Luck,

-Andy
0
 
LVL 3

Accepted Solution

by:
xyu earned 50 total points
ID: 1169803
The trick is here that pressing the <ENTER> you generate IDOK command for Your dialog by default...
The solution is someshing like that:

BOOL CTestDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
      if (wParam == IDOK) return FALSE; // <- Here
      return CDialog::OnCommand(wParam, lParam);
}

This trick works on OWL as well :)
0
 
LVL 5

Author Comment

by:porear
ID: 1169804
Thanks, that solved the problem well (and easily I might add, I knew it could be done simply).  It does trap ALL returns now though, so I have to handle the ones I want myself, which is a little effort but worth it for my app.  

Warmcat's idea worked well too, I did not know about the default button properties (forgive my relative newness to MFC).  I'll post a separate message to give him/her points as well (I have some to offer now that they've finally updated my account). Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 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

21 Experts available now in Live!

Get 1:1 Help Now