Solved

How to cycle through controls using TAB?

Posted on 1998-12-03
10
228 Views
Last Modified: 2013-12-25
On one of my VB forms, I could use TAB to move between
controls according to TABINDEX. But when I come to the
highest TABINDEX, pressing TAB does not return me to the
lowest indexed control. Similar for Shift-TAB, it does not
bring me to the highest index control when I am at
TABINDEX 0. How can I change this?
0
Comment
Question by:angelany
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 2

Expert Comment

by:trillo
ID: 1488681
I don't know which controls you have on your form, but there are several controls (such as PictureBoxes) that accept TabStops but they don't appear to have the focus.... I suggest you to set to False the "tabStop" property of all controls in you form that don't need the input focus.

trillo
0
 
LVL 3

Expert Comment

by:traygreen
ID: 1488682
Ditto on trillo's comments.  Also things like tabs.
If you are having trouble finding out which control it is.....
Put MsgBox ActiveControl.Name in the lost focus for what you believe to be the last control in your tab sequence
run the app...set the cursor in what you believe to be the final control in the tab sequence and press tab.
The message box will tell you the name of the control that now has focus.  Set it's Tabstop to false and continue until the cursor goes where you want it to.
0
 

Author Comment

by:angelany
ID: 1488683
I have UserControls on my form, and they also contain UserControls that accept input,
and VB controls such as buttons.

Usually, if I have all VB controls, when I press TAB, the focus will cycle through all
eligible controls (textbox, button, etc), from tabindex 0, 1, ... to max tabindex, and
one more TAB brings the focus back to tabindex 0. This is visually obvious on the
screen: textbox gets cursor, and button get dashed outline inside the border.

Now what happens to me is that, when I TAB through all controls and come to the
highest tabindex control (e.g. a button), the focus does not move on any more when
I press TAB. It got "stuck". Similar for Shift-TAB when the order is supposed to be
reversed.

I know this should work because some other forms that have similar structure as
this form DO have the TAB key functional as expected. But I could not find out the
difference. Is there anything subtle about UserControls?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 3

Expert Comment

by:traygreen
ID: 1488684
Just curious, but I am assuming there is no code on the control or form to suppress the tab key given focus on that last control?  Esp. since you are saying that the shift tab is not working either.  I had meant to post my answer as a comment, so go ahead and reject it.
If you would like, email me the form traygreen@hotmail.com and I'll take a quick look at it.
Tray
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1488685
you can set the tab  index manually ,

in the last control in the sequence , on the Lost_focus event you can use the GetFocus method of the first control in the sequence.

0
 

Author Comment

by:angelany
ID: 1488686
Setting the tabindex manually does not help. The focuses get stuck
at highest and lowest indeces.

Now my problem is half solved. Since the last item on the form is a command button,
I could use the following code to set focus to the first field, which is a textbox:

Private Sub cmdClose_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyTab Then
        abEffDt.SetFocus
    End If
End Sub

But while I am at the first field (the textbox), pressing Shift-Tab gives me a beep, and
the focus does not change. I can not use lastbutton_setfocus in the code of
firstfield_lostfocus since there are two directions where the focus can go
(TAB and Shift-Tab).

I can not post the form because it contains OCXs and will not show without
some services specific to our setup. I don't think there is anything in the code that suppresses TAB key.

I also noticed that the other forms that I claimed "working" all contain a tabstrip, which
has its tabindex at a higher level, while this form contains one and only UserControl.
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1488687
What you need to do is check the Shift in the KeyDown event.  Your code should look something like this:

If KeyCode = vbKeyTab And (Shift And vbKeyShift) Then
    LastControl.SetFocus
End If

See if that works for you.

Later,
Martin

0
 
LVL 15

Expert Comment

by:ameba
ID: 1488688
angelany, I think your UserControl is having problems. Check property CanGetFocus, ForwardFocus.
It is not unusual to have focus problems with UserControls.

E.g. put two textboxes on usercontrol. When using Tab on your form (which also has textboxes), everything looks OK. But when shift-Tabing, guess what will happen? Try it!

The workaround: replace Usercontrol with normal controls, or put everything in usercontrol, so it is the only control on the form.
0
 
LVL 5

Accepted Solution

by:
AnswerTheMan earned 100 total points
ID: 1488689
perhaps u have a tab index set to an unvisible control or to some other element on your form
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1488690
Sorry, the code I posted earlier has an error.  It should read:

If KeyCode = vbKeyTab And (Shift And vbShiftMask) Then
    MsgBox "Shift Tab"
End If

Good Luck,
Martin
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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