How can I set focus on a field using its tabindex

I would like to control the order in which fields on a form are filled. The order will be conditional / dynamic so the easiest mechanism to control this would be tabindex.

I don’t care or even want to know what the field names are as they have their own validation methods attached I simply need to be able to pass focus to the next valid field.

so I need to be able to say “The field with tabindex 5”.setfocus

I’m not wedded to using the tabindex so if there is a better “generic” way of doing this then please let me know

You can set the focus to any control at any time with Setfocus. See http://documentation.xojo.com/index.php/RectControl.SetFocus

If you want to manipulate the order in which tab passes from one field to another it is possible as well, with TabIndex :
http://documentation.xojo.com/index.php/RectControl.TabIndex

Michel, I understand that but what I’m trying to achieve is not to change the tabindex of the fields but to control the order in which I navigate through them.

So I may have 10 fields on my form with tabindex 1 to 10 (keep it simple).
Depending on what is entered in field 1 (tabindex 1) I may want to go straight to the field with tabindex 7 missing the fields in-between. Then I might want field with tabindex 5 to have focus.

Because I want this to be generic I won’t know what the names of the fields are I just want to be able to select and setfocus based upon their index.

[quote=199555:@David Lane]Michel, I understand that but what I’m trying to achieve is not to change the tabindex of the fields but to control the order in which I navigate through them.

So I may have 10 fields on my form with tabindex 1 to 10 (keep it simple).
Depending on what is entered in field 1 (tabindex 1) I may want to go straight to the field with tabindex 7 missing the fields in-between. Then I might want field with tabindex 5 to have focus.

Because I want this to be generic I won’t know what the names of the fields are I just want to be able to select and setfocus based upon their index.[/quote]

You can use Window.Control http://documentation.xojo.com/index.php/Window.Control to loop through all the controls on the window, and check their TabIndex. For instance :

[code]Sub Action()
Dim c As Control
Dim Msg as String
For i As Integer = 0 To Self.ControlCount-1
c = Window.Control(i)
Msg = Msg+ c.name+ " tabindex : "+str(RectControl(c).TabIndex)+EndOfLine
Next

Msgbox Msg
End Sub
[/code]

With that, you can refer to a control just by its TabIndex. Do not forget the cast to RectControl.

For instance

If RectControl(c).TabIndex = 5 then RectControl(c).SetFocus

Ok let me give that a go - I’ve been trying to do something very basic but similar to try & prove the concept:

    Dim c As Control
    c= Control(Me.tabindex+1)
    RectControl(c).SetFocus

When doing something like this with multiple fields of the same control type, it is always easier to use a control set for the fields. so all of the fields have the same name (i.e. textField1) and each has a unique index number. Then you can do
textField1(5).setfocus
or, of course you can use a variable for the index number
textField1(idxNum).setFocus

[quote=199563:@David Lane]Ok let me give that a go - I’ve been trying to do something very basic but similar to try & prove the concept:

    Dim c As Control
    c= Control(Me.tabindex+1)
    RectControl(c).SetFocus[/quote]

No. That will not work. The order of controls on a window does not necessarily correspond to TabIndex.

It seems you want to focus on the next control in the TabIndex, as if you had pressed Tab. This can be obtained very simply with

Me.SetFocus Self.FocusNext

See http://documentation.xojo.com/index.php/Window.FocusNext

I am not sure if this is what you want, but… here I go:

The control order in a window is set by order of creation on the window.

Say you add three TextFields, then one TextArea, one PopupMenu…
The order will be:

Index # Control kind 0-2 TextField 1, 2, 3 3 TextArea 1 4 PopupMenu

If, you realize that this order is not what you want, you can change it from the Window Editor.

In the IDE, select one Control,
Click in the Inspector (i) Icon,
Click in the teeth wheel (?) the icon at the right of the surrounded ID,
In Focus Control, Field named TabIndex, set the new index value.
Do that for all Controls you want to change their Index value

Once done, run and check if it pleased you.

Also: View Menu, Tab Order MenuItem is your friend. It allows you to make the same changes, but using drag and drop.

I hope I’m correct here.

[quote=199691:@Emile Schwarz]I am not sure if this is what you want, but… here I go:

The control order in a window is set by order of creation on the window.
[/quote]

Emile, Window.Control() order and TabIndex are not the same thing.

Michel,

Sorry for the delay - Yes, that worked thanks. It would be very useful if those jolly nice people at Xojo allowed for an index other than tabindex - it would make dynamic forms a lot easier to create & manage.

Many thanks

[quote=199737:@David Lane]Sorry for the delay - Yes, that worked thanks. It would be very useful if those jolly nice people at Xojo allowed for an index other than tabindex - it would make dynamic forms a lot easier to create & manage.
[/quote]

I do not quite see why you say that. Managing dynamic forms has little to do with TabIndex IMHO.

For dynamic controls, the best way is to use an array into which you keep a reference to all the controls you add/supress.