Posted on 2001-06-08
Last Modified: 2007-11-27
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
                End If
            End If

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

regards Botch.
Question by:Botch
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6168022
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.
LVL 43

Expert Comment

ID: 6168024
If you have an array of textbox controls then all you need to do is this:

txtMyTextBox(x - 3).SetFocus

Expert Comment

ID: 6168095
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

End Sub

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

LVL 22

Expert Comment

ID: 6168562
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.


Accepted Solution

Valliappan AN earned 50 total points
ID: 6172765
Setting the tabindex wont work, as you have done. Since it will overlap with another control with the tabindex with which you set, or else, you need to set the proper tab index affecting the tabindex range that you set.

I mean, I suppose VB does not set the tabindex in runtime automatically as in design time when you change the tabindex for one control, if tabindex lies inbetween another control's tabindex.

A better way is to use SendKeys, like,

if you wish to move 3 tabindexes backwards,


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

If you wish to move front 3 tabindexes do,

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

hope it helps.

Expert Comment

ID: 6174772
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.
LVL 43

Expert Comment

ID: 6176200
Botch, please respond to this thread.

Author Comment

ID: 6176292
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
                ElseIf y < 3 Then
                    ctl.TabIndex = 0
                End If
            End If
    End If

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


Expert Comment

by:Valliappan AN
ID: 6176412
thanks botch and you are welcome for further comments if need be.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question