Solved

How to get focus on a specified control?

Posted on 1998-09-29
8
248 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 50 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

773 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