Link to home
Start Free TrialLog in
Avatar of Botch
Botch

asked on

Tabindex

Hi all
I have a few textbox controls on a form and I want to use the arrow keys to navigate around the form.  The boxs are aligned so that when the user presses the right arrow key,tab is invoked.  Left is a tab backwards effect. These work ok.  However when the user presses on the up arrow they may have to jump 3 tabindexes backwards.  the code I have is as follows

x is read in as the index of the box we are leaving

dim ctl as control

        For Each ctl In Controls
            If TypeOf ctl Is TextBox Then
                If ctl.tabindex = x Then
                    ctl.tabindex = x - 3
                    ctl.SetFocus
                End If
            End If
        Next


This does not work as expected (probably wrong).  Does anybody have any suggestions.  Also why doen't the tab index word take small letters as if it is an incorrect
property.


regards Botch.
Avatar of Joe_Griffith
Joe_Griffith

Resetting the tabindex in code is probably not a good idea.  For one thing two controls can't have the same tabindex (that is probably what is happening here).  I think you should find another way to identifiy the controls.  If they are all the same (text boxes for instance) you should have them in an array.
If you have an array of textbox controls then all you need to do is this:

txtMyTextBox(x - 3).SetFocus
use this one

Private Sub Text1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
on error goto err:
Select Case KeyCode
   
   Case 37
         Text1(Index - 1).SetFocus
       
   Case 39
         SendKeys "{tab}"
End Select
exit sub
err:

End Sub


nikelsh
And why doesn't it "recognize" tabindex (by capitalizing it)?

The answer is that you are redirecting the control into a generic container which has no prior knowledge of the control.  As such it doesn't recognize the tabindex property at design time.  At run-time, the control container picks up the characteristics of the textbox and THEN recognizes tabindex as a valid property.

ASKER CERTIFIED SOLUTION
Avatar of Valliappan AN
Valliappan AN
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HMMM Interesting, I think im gonna agree with Valli, I dont agree with the method and there should be an easier way but because the way that VB handles this key and you cannot cross with another control and the same time his method would be far more safer and less confusing at debug time so im gonna suggest
:-

Sendkeys "+{TAB}+{TAB}+{TAB}"

If you wish to move front 3 tabindexes do,

Sendkeys "{TAB}{TAB}{TAB}"

thats the only safe way to get around this problem without having to worry about any other control

Many Thanks

Mr Microsoft him self.
:op
Botch, please respond to this thread.
Avatar of Botch

ASKER

Thanks for that vialli.  Thats works.
So when I clickon the up arrow the program tabs back 3.  When I click on the up arrow the program tabs up three.
I also tried the other suggestions and its true to say tht if the textboxes are an array the following seems to work
for the up arrow

Dim ctl As Control
    If keycode = 38 Then
        For Each ctl In Controls
            If TypeOf ctl Is TextBox Then
                If ctl.TabIndex = tabidx and tabidx<3 then
                    ctl.TabIndex = tabidx - 3
                    ctl.SetFocus
                ElseIf y < 3 Then
                    ctl.TabIndex = 0
                    ctl.SetFocus
                End If
            End If
        Next
    End If

The sendkeys way is nicer though.  Thanks everybody for all the help.
regards
botch

thanks botch and you are welcome for further comments if need be.

Cheers.