Solved

How to cycle through controls using TAB?

Posted on 1998-12-03
10
229 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

735 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