CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 1999
    Location
    Salisburyl UK
    Posts
    324

    Why does my app close ??

    Hi All....

    Simple question Im sure....
    I have a simple dialog based app that has the usual OK and Quit buttons.
    Ive just put a CEdit box on it and I find that when I press the return key, the app just exits.....
    I need to be able to handle the text in the CEdit box when the user presses the 'return' key.
    Can anyone tell me whats happening and how best to detect the 'return' key press in the edit box ??

    Many Thanks all

    Phill

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Why does my app close ??

    Add the ES_WANTRETURN style to the edit box. Otherwise, the return is handled as an OK by the dialog.


    Edit:

    ES_WANTRETURN is only applicable to multiline edit controls. To handle RETURN with a simple edit box, you need to override OnOK() at the dialog level.
    Last edited by hoxsiew; September 29th, 2009 at 02:25 PM.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Why does my app close ??

    You may want to remove the default property from the ok button too.

  4. #4
    Join Date
    Sep 1999
    Location
    Salisburyl UK
    Posts
    324

    Re: Why does my app close ??

    Hi hoxsiew
    Thanks for your reply.
    Yes.. your right, I need to over ride the OnOK() event.
    Up till now I have used Visual studio 6, Im now using visual Studio 2008.
    I have found the avaliable list of events for the dialog in the properties tab of the 'slide out' panel. However I cant find any way to add a handler for the OnOK() message.
    Any idea how I do this please ??

    Thanks again
    phil

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Why does my app close ??

    Any idea how I do this please ??
    Add a button with identifier 'IDOK'.

  6. #6
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Why does my app close ??

    Quote Originally Posted by Phill Heald View Post
    Hi hoxsiew
    Thanks for your reply.
    Yes.. your right, I need to over ride the OnOK() event.
    Up till now I have used Visual studio 6, Im now using visual Studio 2008.
    I have found the avaliable list of events for the dialog in the properties tab of the 'slide out' panel. However I cant find any way to add a handler for the OnOK() message.
    Any idea how I do this please ??

    Thanks again
    phil
    You can find OnOK in the Virtual Functions tab. OnOK and OnCancel are virtual functions and not message handlers.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Why does my app close ??

    Your dialog will typically also close upon hitting the Esc. key.

    Override OnCancel() and make it do nothing to prevent this behaviour.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Why does my app close ??

    The question asked is how does he prevent it closing when he presses the enter key. Overriding OnOk won't help that problem as it will be triggered by the mouse too. He needs to remove the default property from the OK button, and as already suggested, add Want Return style to the edit control.

  9. #9
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Why does my app close ??

    Overriding OnOK and OnCancel with empty functions, without calling CDialog::OnOK and CDialog::OnCancel, prevents dialog from closing by pressing Enter and Esc.
    In this particular case, ES_WANTRETURN is enough.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Why does my app close ??

    Not entirely. ES_WANTRETURN changes the behaviour of the editcontrol in that you can actually use enter to generate a newline in your editcontrol. In a multiline edit, this can be an important behavioral change. It could be what he wants, but it could also NOT be what he wants.

    It may appear to solve the issue, but it's not the 'right' solution to this problem. If the dialog gets changed to have another control that is not an editcontrol, you're back to square 1 since pressing enter when the other control is active will yet again close the dialog.

    So the right solution:
    If you have NO ok button -> override OnOk to do nothing.
    if you have an OK button -> do not give OK (or any other button) the default button style

    Esc closing the dialog has solutions also... albeit different ones...
    If you have no Cancel button -> override OnCancel to do nothing
    If you have a Cancel button -> Create an accellerator for Esc that maps to a dummy handler.


    The above guideline will make sure normal behaviour still works when you have buttons and when those buttons have the focus and you hit enter (in which case you DO want them to execute the OK/Cancel handlers).

  11. #11
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Why does my app close ??

    In YourClassDialog::OnOK, you can use GetFocus to determine if control is on OK button or not:
    Code:
    if ( GetFocus() == GetDlgItem(IDOK) )
        ;// process actual OK
    else
        ;// Find if the focus was on desired control, and act appropriately.
    If you have multiple controls, and want to move to next control (like we do using Tab key), you can use NextDlgCtrl.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  12. #12
    Join Date
    Sep 1999
    Location
    Salisburyl UK
    Posts
    324

    Re: Why does my app close ??

    guys.. thanks for your replys..
    More of a discussion here than I was expecting !

    Ive found the virtual functions and over ridden the OnOK and OnCancel functions.
    It now seems that I cant close the app at all ! the 'X' button and the quit button as well as the 'esc' button are non responsive.

    I need to handle the 'return' button press so I know when to parse the contents of the CEdit box (single line only).

    Ajay, could I use your suggestion to trap the 'return' key press and handle the edit box contents there, also could I do something similar with the 'esc' key press ??

    Thanks all
    Phil

  13. #13
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Why does my app close ??

    Don't override OnCancel - it is preventing you from closing the dialog box. If you must, you can:
    Code:
    void YourClass:OnCancel()
    {
    int nResponse = AfxMessageBox(...);  // Ask the user for confirmation
    if ( nResponse == confirm )
        CDialog::OnCancel();
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  14. #14
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Why does my app close ??

    Ajay, could I use your suggestion to trap the 'return' key press and handle the edit box contents there, also could I do something similar with the 'esc' key press ??
    OnOK and OnCancel implementations are for different purpose, though they close the dialog box eventually. The default implementation goes like this:

    CDialog::OnOK() - EndDialog(IDOK)
    CDialog::OnCancel() - EndDialog(IDCANCEL)
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured