We help IT Professionals succeed at work.

New to Visual Basic

Medium Priority
253 Views
Last Modified: 2013-11-25
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.
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

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".
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.