CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Guest

    Pressing Enter in Dialog

    I have a dialog that has multiple edit boxes on it. This dialog opens when a button is pressed. These edit boxes are set up to show at different times on a tab control. The problem I am having is when I press enter on any of the edit boxes the dialog closes. How do I stop the dialog from closing or hook the enter key.




  2. #2
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Pressing Enter in Dialog

    Your problem relates to the default button. When you press enter, MFC routes the enter key as a click on the default button (which is by default the OK button, ID of IDOK) and your dialog boxes OnOK() function gets called.
    You can get around this by overriding the default OnOK() and make sure it doesn't call the CDialog::OnOK() function (which closes the dialog box). Then when you do need to close the dialog box, you can have your own OK button that calls the CDialog::OnOK() function as required.


    Roger Allen
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  3. #3
    Join Date
    Mar 1999
    Location
    St. Louis
    Posts
    45

    Re: Pressing Enter in Dialog

    I have a similar problem- maybe you can help.
    I have a dialog with a default button. Return key does not call the OnOK. Is there another flag to set to enable that processing? The dialog is modal with a list box, password field, and OK, and Cancel buttons.

    Dave


  4. #4
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Pressing Enter in Dialog

    From what I remember, if you have a default button set, which is not the OK button, then pressing enter will cause windows to assume a press of that button. If you need to route it through the OK button, you will either have to make the OK button your default button, or you will have to override the PreTranslateMessage for you dialog box and trap the ENTER key to do the processing you require at the right time. Example code is:


    BOOL CDialogClass::PreTranslateMessage(MSG* pMsg)
    {
    if (pMsg->message == WM_KEYDOWN)
    {
    TRACE("%d %d\n", pMsg->wParam, pMsg->lParam) ;
    switch (pMsg->wParam)
    {
    case VK_ENTER : // enter
    // do you processing fro special occurance here
    // return true if you dealt with the message
    return true ;
    }
    }
    return CDialog::PreTranslateMessage(pMsg);
    }





    HTH


    Roger Allen
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

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