Visual Basic Helper Routines
Pure VB: Determine the Selected Button in a Option Button Control Array
     
Posted:   Sunday December 07, 2003
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   Rick Rothstein, VBnet - Randy Birch
     

Related:  

GetSelectedOptionIndex
     
 Prerequisites
None.

While GetSelectedOptionIndex shows a handy method to retrieve the index of the selected item when using an option button control array, due to the hard-coding of the control array name the method is restricted to just that control array. Using this technique when a form contains many option control arrays requires a separate procedure for each control name.

Although the method below, based on a post in the msnews groups by Rick Rothstein, uses the Object data type and therefore late binding, it does provide additional flexibility over the abovementioned routine in that it accepts any option button control array on the form.

As coded, if no option buttons are selected the routine returns -1 to indicate no selection. The routine also returns -1 if an incorrect control type is passed (for example an array of text boxes rather than option buttons) something that theoretically should never happen as you have command of the application code.

 BAS Module Code
None.

 Form Code
To the form, add one option button (Option1) and set the control's Index property to 0 to create an control array; the Load event will create a few more for testing. Add two command buttons (Command1 and Command2) and the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_Load()

  Dim cnt As Integer
  Dim nControls As Long
  
  nControls = 6
  
  For cnt = 0 To nControls - 1
   
     'control(0) already exists; don't try to load it!
      If cnt > 0 Then Load Option1(cnt)
      
     'position and show
      With Option1(cnt)
         .Move 300, 200 + (cnt * 300), 3500, 285
         .Caption = "Option button - index #" & cnt
         .Visible = True
      End With
   Next
   
   Command1.Caption = "What's Selected?"
   Command2.Caption = "Deselect All"
   
   Command1.Move Option1(0).Left, _
                 Option1(nControls - 1).Top + _
                 Option1(nControls - 1).Height + 230
                 
   Command2.Move Command1.Left + Command1.Width + 50, _
                 Command1.Top, _
                 Command1.Width, _
                 Command1.Height

End Sub


Private Sub Command1_Click()

   MsgBox "The button selected is index #" & GetSelectedOptionIndex2(Option1)

End Sub


Private Sub Command2_Click()

   Dim ctl As Control
   
   For Each ctl In Me
      If TypeOf ctl Is OptionButton Then
         ctl.Value = False
      End If
   Next
   
End Sub


Private Function GetSelectedOptionIndex2(OB As Object) As Long

   Dim ctl As OptionButton
  
  'default return value if nothing
  'is selected
   GetSelectedOptionIndex2 = -1
  
   If VarType(OB) = vbObject Then
      If TypeOf OB(OB.LBound) Is OptionButton Then
    
         For Each ctl In OB
            If ctl.Value Then
               GetSelectedOptionIndex2 = Abs(ctl.Value * ctl.Index)
               Exit For
            End If
         Next
      
      End If  'If TypeOf
   End If  'If VarType

End Function
 Comments
The magic of this routine is that the selected option button's .Index (0 through 5 above), when multiplied by the selected option button's current .Value (either 0 or -1 (True)), results in only one combination having a non-zero value. Assuming that the Option button with index 3 was selected:
   
ctl.Index  
ctl.Value  
ctl.Value * ctl.Index
 
Abs(ctl.Value * ctl.Index)
0 x
0 (False) =
0
  
0
1 x
0 (False) =
0
  
0
2 x
0 (False) =
0
  
0
3 x
-1 (True) =
-3
  
3
4 x
0 (False) =
0
  
0
5 x
0 (False) =
0
  
0

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter