Mystogan
Mystogan

Reputation: 577

Programmatically selecting second control in tab order

I have a dialog with user controls and is trying to set the control second in line in the tab order to be the starting tab. I.e. when the form is activated the focused control is not actually the one with lowest tab order. I tried to set this in the constructor using

  this.SelectNextControl(this.ActiveControl, true, true, true, false);

but 'this.ActiveControl' is null at that time.

anyone got ideas?

Upvotes: 0

Views: 1871

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112782

Try

tabControl1.SelectTab(1);

or

tabControl1.SelectTab("tabPage2");

Both versions work, when called in the constructor immediately after InitializeComponent(). (tested)


EDIT (after your clarification)

Make this (now second) control the first in the tab order and the previous control (now the first one) the last in the tab order. Since pressing the tab key cycles through all the control, automatically restarting at the beginning after having reached the last one, you should get the desired behavior.

+---+  +---+  +---+  +---+
| 4 |  | 1 |  | 2 |  | 3 |
+---+  +---+  +---+  +---+

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56747

Try that in the Shown event. This event is invoked as the window is actually shown - at that point, all focussing is already done, all controls have been created and the form is ready. This may not be the case in constructor or Load.

Upvotes: 1

Memet Olsen
Memet Olsen

Reputation: 4608

Maybe this will do it:

tabControl1.SelectedIndex = 1;

Upvotes: 1

Related Questions