Link to home
Start Free TrialLog in
Avatar of adriankohws
adriankohwsFlag for Singapore

asked on

New to Visual Basic

Hi Experts,

I am brand new to Visual Basic.

I have this function here whereby it will execute anytime user hit on "F2".

I want the focus to set on the next "Tabindex". I do not know the properties well, can someone assist?

I know how to achieve by indicating for every single control but I sounds stupid. It should have a way to identify where is the tabindex on focus now and is just the matter of adding 1 to navigate to the next one.

Thanks advance for your assistance.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Something like...

Option Explicit

Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF2 Then
        SendKeys "{Tab}"
    End If
End Sub

Avatar of adriankohws

ASKER

Thank you. Actually, if I want to use this, of course it will work. But my real concern is not to just get it work.

My main purpose is to know how to access the properties of the controls within a form without having to name the controls like "To find out which control is currently focused or activve".
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America 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
Well, something very close...

Just wondering if I can get what is the tabindex of the activecontrol and add one to it to navigate to the next control?
I don't know why you would want to do it this way...

Option Explicit

Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim ctl As Control
    Dim curCtl As Control
    Dim curTabIndex As Integer
    Dim firstCtl As Control
   
    If KeyCode = vbKeyF2 Then
        Set curCtl = Me.ActiveControl
        If Not (curCtl Is Nothing) Then
            curTabIndex = curCtl.TabIndex
            For Each ctl In Me.Controls
                If Not (ctl Is curCtl) Then
                    If ctl.TabIndex = (curTabIndex + 1) Then
                        ctl.SetFocus
                        Exit Sub
                    ElseIf ctl.TabIndex = 0 Then
                        Set firstCtl = ctl
                    End If
                End If
            Next
            If Not (firstCtl Is Nothing) Then
                firstCtl.SetFocus
            End If
        End If
    End If
End Sub