Setting Focus to Desired Control on a TabPage

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?
 
H

Herfried K. Wagner [MVP]

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.
 
P

PvdG42

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top