Solved

How to get focus on a specified control?

Posted on 1998-09-29
8
246 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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1253092
case PSN_SETACTIVE:
SetFocus( ...etc.. ) ;
            break;
0
Are your end users making ugly email signatures?

Have you left it up to your end users to create their own email signatures? Are they forgetting to add the company logo or using garish font colors? Take control and ensure all users have the same email signature.

 

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

Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

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…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now