Quantcast
Channel: Visual C++ Tips
Viewing all articles
Browse latest Browse all 10

How to set Focus to Different Control on Dialog Startup?

$
0
0

Do you want to set the focus to another control on displaying Dialog? Or tried SetFocus() to another control in OnInitDialog() and want to know why its not working? The answer for your ‘Focus’ question is here.

If you are setting the default focus to another control in dialog, then OnInitDialog() should return FALSE. Have a look at the code snippet below.

BOOL CStartupFocusDlg::OnInitDialog()
{
 ...
 // Set focus to your control.
 CWnd* pWnd = GetDlgItem(IDC_EDIT2);
 pWnd->SetFocus();

 // return TRUE;  // Wizard Generated code.
 // Return FALSE if you set focus to different control
 return FALSE;
}


Download the Sample, if you want to see it in action. Please note that sample is compiled in Visual C++ 2008.


Viewing all articles
Browse latest Browse all 10

Trending Articles