CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 1999
    Location
    Canada
    Posts
    176

    CEdit and Return Key

    Hi,
    I have an EditBox and a button control. I want changes in editBox to be applied (but calling OnButtonClick)
    when the user hits the return key. The problem is that, as the Ok button is the default button, the dialog is closed
    as soon as we press enter. I tried to change the button style (using BS_DEFPUSHBUTTON) but this doesn't
    work. I tried also to subclass the edit control and trap the VK_RETURN in the onChar() member function but
    this doesn't work either.

    Can you help me?

    --
    Where there is a WISH, there is a WILL.

  2. #2
    Join Date
    May 1999
    Posts
    667

    Re: CEdit and Return Key

    Override PreTranslateMessage and add the following code

    BOOL YourDialog::PreTranslateMessage(MSG* pMsg)
    {
    if (pMsg->message == WM_KEYDOWN)
    {
    if (pMsg->wParam == VK_ENTER)
    {
    OnBtnClicked(); // you could also use post message but this works
    return TRUE;
    }
    }
    return yourbaseclass::PreTranslateMessage(pMsg);
    }

    HTH,
    Chris


  3. #3
    Join Date
    May 1999
    Posts
    667

    Re: CEdit and Return Key

    Sorry,
    you need to test for the edit control being the window

    so make the following change to the function
    if (pMsg->wParam == VK_ENTER)
    {
    if (pMsg->hwnd == ::GetDlgItem(IDC_EDIT))
    {
    OnBtnClicked(); // you could also use post message but this works
    return TRUE;
    }
    }


    HTH,
    Chris


  4. #4
    Join Date
    May 1999
    Location
    Canada
    Posts
    176

    Re: CEdit and Return Key

    Thanks. The only problem is that ::GetDlgItem does not take one parameter and and don't know how to obtain the handle of a control (or dialog).

    --
    Where there is a WISH, there is a WILL.

  5. #5
    Join Date
    Oct 1999
    Posts
    16

    Re: CEdit and Return Key

    Try GetDlgItem without the scope resolution operator (:. Much simpler.
    GetDlgItem(IDC_EDIT);


    If, however, you must use ::GetDlgItem, you can get the HWND by calling GetSafeHwnd.

    The code would then look like this:
    ::GetDlgItem(GetSafeHwnd(), IDC_EDIT);




  6. #6
    Join Date
    May 1999
    Location
    Canada
    Posts
    176

    Re: CEdit and Return Key

    Thank you Rabit-toaster, thank you ChriD. I thank you a lot. Everything is working just fine now.

    --
    Where there is a WISH, there is a WILL.

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