Thread: Focusing a CEdit Object in a Dialog On Startup :: MFC

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Focusing a CEdit Object in a Dialog On Startup :: MFC

    Hi.

    I have a dialog box. When Windows draws the dialog box, I want to to focus to a specific CEdit object (window). For example, let say the dialog box asks the user for a username and password. I would like Windows to focus the username window when it first draws the dialog box.

    What is the best way to do that?

    Thanks,
    Kuphryn

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I don't use mfc but here goes...

    You should have an OnInitDialog function (WM_INITDIALOG handler). In this function you can use the SetFocus method of the CEdit wnd to 'claim' the focus or you can use the API function ::SetFocus(hEdit) where hEdit is the handle of your edit control (m_hWnd member variable).

    Whichever approach you take return FALSE (0) from the OnInitDialog function (WM_INITDIALOG handler) or the system will set the default focus itself.

    Hope that helps.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    I presume the coding technique applies to modal dialog box and modeless dialog box.

    Kuphryn

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I presume the coding technique applies to modal dialog box and modeless dialog box.<<

    Yes.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    I saw at least two responses about overriding OnInitDialog().

    Here is the solution:

    ...OnInitDialog()
    {
    ...
    GetDlgItem(the CEdit window's ID)->SetFocus();
    ...

    return FALSE;
    }

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC Save As dialog
    By Micko in forum Windows Programming
    Replies: 0
    Last Post: 08-23-2004, 02:02 PM
  2. MFC Dialog :: Slider Control child notification messages?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2003, 12:09 PM
  3. Accessing CEdit Member Functions Inside CEditView ::MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-10-2002, 08:52 PM
  4. Setting Object Data *double* For Class ComboBox :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 03-24-2002, 06:20 PM
  5. Replies: 1
    Last Post: 09-17-2001, 05:46 AM