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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Oder on form 1

Status
Not open for further replies.

judyscofield

Programmer
Sep 24, 2001
56
US
How do I change the tab order of ojects on a form? The help screens say to use the layout menu, but I don't see a layout menu anywhere on the screen.
 
The easiest way to reorder the Tab sequence is to manually select the objects in reverse order (select the last control your users will select on the form) and set the index to zero for each object. For instance, select an Exit button, set the tab index to zero, then select a text box and set it's tab index to zero--this renumbers the tab index of the button to 1. The text box control will be the first item visted (tab index 0) and then when the tab key is hit, will transfer to the button (tab index 1).

Mark
 
That's brilliant. I'd never thought of doing it that way. Thanks Mark. Peter Meachem
peter@accuflight.com

 
but ... you also need to be aware of some trivia in the 'taborder'. An important aspect is that when "Labels" are included in the tab order (and they ARE by default), when they are tabbed to, the next control in the tab order actually receieves the focus.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Mark, this works great - thanks. I'm still a little curious as to how to get to the layout screen that is described as having this objects listed and being able to move them up and down in position. Judy
 
That's an Access utility (from the Top Line menu- View, Tab Order) that allows you to see the objects by name and reorder the Tab Index property graphically. It's not available in VB (at least not in VB5 or 6), but there may be an add-in somewhere that can give you this capability.

Mark
 
Just to go a bit further:
If you often re-order the controls, or are adding/removing controls often, then it may be best to set the tab order in the Form_Load event, hard coding it. Then, you have a good overview and can make changes easily.

Private Sub Form_Load()
Text1.TabIndex = 0
Text2.TabIndex = 1
Text3.TabIndex = 2
Text4.TabIndex = 3
Text5.TabIndex = 4
End Sub

This may mean more work at first, but in the long run, esp. for forms with alot of controls, it will make things easier.

Now, before some of you say that it is just as much trouble to re-number 75 controls in code, when adding just one control in the second tab position, let me add to MarkSweetland's example, and my example above:

Private Sub Form_Load()
Text5.TabIndex = 0
Text4.TabIndex = 0
Text3.TabIndex = 0
Text2.TabIndex = 0
Text1.TabIndex = 0
End Sub

Now, you do not have to re-number anything. If I add Text6 and want it to be in the second tab order I just add the line of code where I want it:

Private Sub Form_Load()
Text5.TabIndex = 0
Text4.TabIndex = 0
Text3.TabIndex = 0
Text2.TabIndex = 0
Text6.TabIndex = 0
Text1.TabIndex = 0
End Sub

So, I do not need to re-number anything, and have a list right in front of me to easily review......and make changes with.
If you get into the habit of doing this, then you will also find that in addition, you also have a list available in which you can copy control names from, when you need to set properties or use a control - or more beneficial - several controls, elsewhere in code.

How do I get a list of the controls that are on my form in a fast and easy manner - with-out going to the trouble of creating(and learning to create) an add-in? Especially if there are many controls?
And then with that list set the Tab order property at once, ignoring the controls which do not have Tab properties, or those which have a TabIndex but no TabStop property (like a label)?
Create this small proceedure, paste it into the code window of a module, open the Debug/Test window and call it, then copy the list from the Debug window and paste it to the FormLoad event. Once there, you can set the order as desired:

Public Sub ListControlTabStops(frm As Form)
Dim c2 As Control
Dim c1 As Control
Dim iMaxTabIndex As Integer

'First, find the control with the highest tab index
For Each c1 In frm.Controls

On Error Resume Next

'Some controls do not have a TabStop, or Tab properties, and some controls, like a lable, have a TabIndex but no TabStop. These, we can ignor.
If c1.TabStop Then
If Err = 0 Then If c1.TabIndex > iMaxTabIndex Then iMaxTabIndex = c1.TabIndex 'control has a TabStop and TabIndex
End If
Err.Clear
Next c1

On Error Resume Next
For Each c1 In frm.Controls
For Each c2 In frm.Controls
If c2.TabStop Then
If Err = 0 Then 'control has a TabStop
If c2.TabIndex = iMaxTabIndex Then
iMaxTabIndex = iMaxTabIndex - 1
'Debug.Print c2.Name & ".TabIndex = 0" & " 'Tab Index=" & c2.TabIndex

Debug.Print c2.Name & "(" & c2.index & ").TabIndex = 0"
If Err = 343 Then Debug.Print c2.Name & ".TabIndex = 0"; 'Control is not an array
Exit For
End If
End If
Err.Clear
End If

Next c2
Next c1
End Sub
Call it from the Debug window as such:
ListControlTabStops Form1

One thing to note on: You may be asking why there are two loops. The reason is so that we can get the current order of tab stops that the controls on the form are initially in - and that needs to also be in reversed order.
Then, if the tab order of all controls on the form is currently correct, the list will also be correct. If not, then you will need to change the order of the print-out into the order that you want (in reverse order ofcourse)!

I guess this could have been created as an Add-In so that it could be ran from the VB IDE menu. Then the new tab stop code would automatically be placed into a Form Load event. One step further than even that would be to have a 2nd add-in that would also do this for all forms.

 
Mark, thanks - I must have had all areas checked in my msdn search.

Cclint - great idea and it seems well worth the effort if the form has a lot of controls.

Judy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top