Setting Focus to Desired Control on a TabPage

  • Thread starter Thread starter PvdG42
  • Start date Start date
P

PvdG42

VS 2008 VB Winforms project, using a TabControl with several tab pages.

For data capture purposes, I need to set the focus on any given page to a
particular TextBox. I have tried the following without success:

1. Set Tab Order for each tab page at design time.
2. Call <controlName>.Focus() in the Form_Activated event procedure.
3. Call <controlName>.Focus() in the <TabControlName>.GotFocus() event
procedure.
4. Call <controlName>.Focus() in the <TabPageName>.GotFocus() event
procedure.

No matter what I do, the behavior is the same, so there must be something
I'm missing.
Behavior: When any given TabPage is brought to the foreground, there is no
visible sign as to what has focus, but when I hit Tab one time, the desired
control get the focus. I need to eliminate the need for the keystroke or
simulate it (which just occurred to me).

Trying SendKeys.Send(Keys.Tab) doesn't do it either.

Any suggestions?
 
PvdG42 said:
VS 2008 VB Winforms project, using a TabControl with several tab pages.

For data capture purposes, I need to set the focus on any given page to
a particular TextBox. I have tried the following without success:

1. Set Tab Order for each tab page at design time.
2. Call <controlName>.Focus() in the Form_Activated event procedure.
3. Call <controlName>.Focus() in the <TabControlName>.GotFocus() event
procedure.
4. Call <controlName>.Focus() in the <TabPageName>.GotFocus() event
procedure.

No matter what I do, the behavior is the same, so there must be
something I'm missing.
Behavior: When any given TabPage is brought to the foreground, there is
no visible sign as to what has focus, but when I hit Tab one time, the
desired control get the focus. I need to eliminate the need for the
keystroke or simulate it (which just occurred to me).

You may want to call the control's 'Select' method instead of the
'Focus' method. 'Focus' will only work if the control is both enabled
and visible.
 
Herfried K. Wagner said:
You may want to call the control's 'Select' method instead of the 'Focus'
method. 'Focus' will only work if the control is both enabled and
visible.

Beautiful! Works like a charm. Thanks very much.
 
Back
Top