Solved

How to get focus on a specified control?

Posted on 1998-09-29
8
244 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

706 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

19 Experts available now in Live!

Get 1:1 Help Now