I created an application in which I use window procedure to keep track of all the controls in the window.
My question is, how do I initially set the focus to the first created control in the window?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are two ways to set the initial focus to a particular control in MFC.
The first, and simplest, method is to take advantage of your controls’ tab order. When you use the Resource Editor in Visual Studio to lay out a dialog, you can assign each control a tab index. The control with the lowest tab index will automatically receive the initial focus. To set the tab order of your controls, select “Tab Order” from the “Format” menu, or press Ctrl+D.
The second, slightly more complicated, method is to override the
OnInitDialogfunction in the class that represents your dialog. In that function, you can set the input focus to any control you wish, and then returnFALSEto indicate that you have explicitly set the input focus to one of the controls in the dialog box. If you returnTRUE, the framework automatically sets the focus to the default location, described above as the first control in the dialog box. To set the focus to a particular control, call theGotoDlgCtrlmethod and specify your control. For example:Note that you should not set focus in a dialog box by simply calling the
SetFocusmethod of a particular control. Raymond Chen explains here on his blog why it’s more complicated than that, and why theGotoDlgCtrlfunction (or its equivalent, theWM_NEXTDLGCTRLmessage) is preferred.