Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get focus on a specified control?

Posted on 1998-09-29
8
Medium Priority
?
265 Views
Last Modified: 2010-04-15
How to programmatically get focus on the specified control of a dialog? (eg. editbox).
0
Comment
Question by:softyan
  • 5
  • 2
8 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1253089
Assuming C with Windows SDK / API

/* hWndEdit is handle of control */
SetFocus( hWndEdit ) ;

OR

/* hDlg = the dialog, ID_EDIT = control id */
SetFocus( GetDlgItem( hDlg, ID_EDIT ) ) ;

If you are doing this in response to the WM_INITDIALOG message you should return FALSE (rather than TRUE) if you are setting the focus yourself, rather than letting Windows do it for you (TRUE means Windows will pick the control to focus based on the dialog template)
0
 

Author Comment

by:softyan
ID: 1253090
Currently, i'm using a property sheets as a wizard. I have try it your way, it seems effortless, the focus always get at the first edit box. Did i miss out something?
0
 

Author Comment

by:softyan
ID: 1253091
Sorry for not briefly discribe my situation.

Currently i have created a wizard dialog which contains 3 pages.
I want the second page to do some verification when pressing the NEXT BUTTON on it. If success goto third page, else remain within and focus to a specified control. Here is my code segment of the second page:

switch (message)
{
    case WM_INITDIALOG:
      break;
    case WM_NOTIFY:
        switch (((NMHDR FAR *) lParam)->code)
          {
      case PSN_KILLACTIVE:
            break;
      case PSN_RESET:
            break;
      case PSN_SETACTIVE:
            break;
        case PSN_WIZBACK:
            break;
        case PSN_WIZNEXT:
//At here I need to perform some verification, if fail remain same page focus on e.g. IDC_EDIT3, else forward.
            if (A)
          {
          }
          break;
      default:
          PostQuitMessage(0);
          return FALSE;
     }
     break;
     default:
      return FALSE;
}


Where should i put the setfocus on that control?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Answers2000
ID: 1253092
case PSN_SETACTIVE:
SetFocus( ...etc.. ) ;
            break;
0
 

Author Comment

by:softyan
ID: 1253093
i tried it out but it just same as before.
0
 

Author Comment

by:softyan
ID: 1253094
....
0
 

Accepted Solution

by:
cgod earned 100 total points
ID: 1253095
If you're dynamically creating the pages (creating the next page when the Next button is clicked), try this...

case WM_INITDIALOG:
  SetFocus(GetDlgItem(hdlg,item_identifier));
  return FALSE;

But, if you're creating them all at once when you create the main dialog (which is what it sounds like), call SetFocus in the WM_COMMAND/BN_CLICKED for the Next button.
0
 

Author Comment

by:softyan
ID: 1253096
It still not working. The result are same as i try it out on  Answers2000's suggestion.
For a clearer description, i got 3 edit controls on the second page. If i click the next button of the second page, my program will go through some velidation among this 3 edit controls. If the value of the second edit control cannot pass through the validation, then i want to set the focus on the second edit control. If the third edit control cannot pass the validation, the focus will set on the third edit control. But I had tried all the suggestion above, the focus will set on the first edit control regardless which edit control fail the test.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question