Search code examples
vb6radio-buttontabindex

Tabbing between radio buttons in VB6


I have a form which consists of six radio buttons within a frame which are mutually exclusive and one command button.

I already gave different tab-index to each radio button but at the run time by pressing tab focus skipped out of the radio buttons.

so how to give focus to another radio button by pressing TAB?


Solution

  • Private Sub Option1_KeyPress(KeyAscii As Integer)
       If KeyAscii = 9 Then
          Option2.SetFocus
       End If
    End Sub
    

    KeyAscii=9 is the code for the Tab key. But you must do it for all of your radio buttons.

    If you add your radio buttons belonging to the same radio button having indices 0, 1, 2 you can do it like this:

    Private Sub Option1_KeyPress(Index As Integer, KeyAscii As Integer)
    If KeyAscii = 9 Then
        If Index < Option1.Count - 1 Then
            Option1(Index + 1).SetFocus
        Else
            Option1(0).SetFocus
        End If
    End If
    End Sub