Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic tab ordering

Status
Not open for further replies.

sarahnice

Technical User
Nov 18, 2002
101
IE
Hi,

I have a windows form with numerous textfields and a button. When the form is loaded, some of the textfields are visible, but not all, along with the button. The first textfield has focus and when i hit tab, the button is highlighted (the first textfield has tabIndex = 1 and the button has tabIndex = 2).

When the button is clicked, some of the textfields become invisible while others are invisible. After the button is clicked, a different text field has focus and i want to be able to hit tab once and highlight the same button as before.

I've tried to set the TabIndex property of the button to a different value in the onClick method of the button but it doesn't seem to work.

Basically, the form can be in onw of two states. In each state, a particular textfield is highlighted and i want to highlight the button with one tab regardless of which state the form is in.

I know i haven't really explained this well but any help would be greatly appreciated.

Thanks :)
 
Microsoft seems to have really set the wheels in motion when they got rid of the controls array. TabIndex no longer works quite like it dod before. It is possible to have multiple controls with the same TabIndex. In that case, the system uses the z-order, which I haven't found a way to manipulate at runtime.

Here is one painful solution:

Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.Name
Case "Button3"
ctl.TabIndex = 0
Case "Button2"
ctl.TabIndex = 1
Case "Button1"
ctl.TabIndex = 2
Case "TextBox1"
ctl.TabIndex = 3
Case "TextBox2"
ctl.TabIndex = 4
Case "RoundButton1"
ctl.TabIndex = 5

End Select

Next

This gives me a giant case statement where I must set each TabIndex to what I want based on its name. The only redeeming feature of using the controls array is that if there are any that I don't need to change, I don't have to include them.
It's far from elegant, but it does the job.
 
You can use the text box leave event and set the focus to the button in there.

E.g.,

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Button1.Focus()
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top