[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

How to cycle through controls using TAB?

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
angelany
Asked:
angelany
  • 2
  • 2
  • 2
  • +4
1 Solution
 
trilloCommented:
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
 
traygreenCommented:
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
 
angelanyAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
traygreenCommented:
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
 
InteqamCommented:
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
 
angelanyAuthor Commented:
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
 
mkmccrearyCommented:
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
 
amebaCommented:
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
 
AnswerTheManCommented:
perhaps u have a tab index set to an unvisible control or to some other element on your form
0
 
mkmccrearyCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now