CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Set focus to control in CPropertyPage

    Hello!

    I have some CPropertyPages in a CPropertySheet (no wizard, just tabs). The CPropertyPages contain a lot of edit- and listbox-control.

    What I want:
    - At start, I want to set the focus to a special control, which should NOT be first in tab-order
    - I want to save the control which has focus so that when the users gets back to the page, the focus is on the same control.

    Both is just not working, the focus is always at the control with tab-stop 1 when initialising the page/jumpung to the page.

    I tried:
    ::OnInitDialog()
    - Setting the focus with SetFocus()/GotoDlgCtrl()
    - returning FALSE
    => The focus is still at the first control.

    ::OnSetActive
    - Setting the focus with SetFocus()/GotoDlgCtrl()
    => The focus is still at the first control.

    ::OnSetFocus
    - Setting the focus with SetFocus()/GotoDlgCtrl()
    => The focus is still at the first control.

    Any other ideas?
    Last edited by martho; November 25th, 2002 at 05:42 AM.

  2. #2
    Join Date
    Dec 2001
    Posts
    121

    Re: Set focus to control in CPropertyPage

    I encount the same problem, who can tell me why?

  3. #3
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Set focus to control in CPropertyPage

    And are you doing the same thing in your app too ?

  4. #4
    Join Date
    Dec 2001
    Posts
    121

    Re: Set focus to control in CPropertyPage

    I've tried to use SetFocus() in OnInitialDialog() and OnSetActive(), neither works.

  5. #5
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Set focus to control in CPropertyPage

    What kind of control are you trying to set focus on ?
    Also, does the same happen with a sample App ?

  6. #6
    Join Date
    Dec 2001
    Posts
    121

    Smile Re: Set focus to control in CPropertyPage

    OK, I search out an article in MSDN which discusses the problem:

    How to Change Default Control Focus on CPropertyPage
    Last reviewed: October 10, 1997
    Article ID: Q148388
    4.00 WINDOWS NT kbprg kbhowto kbcode
    The information in this article applies to:

    The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++, 32-bit Edition, version 4.0


    SUMMARY
    When a CPropertyPage is activated, the first control in the page's tab order will get the focus by default. To change the default to a different control, derive a class from CPropertyPage, override OnSetActive(), and post a user message to a member function that will actually set the focus to the desired control.


    MORE INFORMATION
    CPropertySheet's member function OnSetActive() is called when a PSN_SETACTIVE message is sent to that page. Attempting to set the focus to a control while in this handler won't produce the expected results because after this notification is handled, the tab control will get the focus.

    Therefore, you need to post a message to the property sheet's message queue with a user message in order to set the focus to the desired control once the page is active.



    Sample Code

    // this is the user-defined message
    #define WM_SETPAGEFOCUS WM_APP+2

    BEGIN_MESSAGE_MAP(CMyPage, CPropertyPage)
    ON_MESSAGE(WM_SETPAGEFOCUS, OnSetPageFocus)

    END_MESSAGE_MAP()
    BOOL CMyPage::OnSetActive() {

    BOOL fRet = CPropertyPage::OnSetActive();

    PostMessage(WM_SETPAGEFOCUS, 0, 0L);

    return fRet;

    }
    LONG CMyPage::OnSetPageFocus(UINT wParam, LONG lParam) {

    // IDC_BUTTON2 is the button on this property page
    // that you want to give the default focus.

    CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON2);
    ASSERT(pBtn);
    pBtn->SetFocus();
    return 0;
    }

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Set focus to control in CPropertyPage

    Useful info... thanks for sharing..

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