• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

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.
0
adriankohws
Asked:
adriankohws
  • 3
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
adriankohwsAuthor Commented:
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".
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
To get the control that is currently focused:

    Dim c As Control
    Set c = Me.ActiveControl
    If Not (c Is Nothing) Then
        ' do something with "c" in here...
    End If

To iterate thru all the controls on a Form:

    Dim c As Control
    For Each c In Me.Controls
        ' do something with "c" in here...  
    Next c
0
 
adriankohwsAuthor Commented:
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?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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