?
Solved

How to get focus on a specified control?

Posted on 1998-09-29
8
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

718 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