Microsoft KB Archive/88910

= How to Determine Which Option Button is Selected in VB =

Article ID: 88910

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q88910



SUMMARY
This article describes a suggested method for determining which one of a group of option buttons is selected.



MORE INFORMATION
You can do the following to determine which option button is selected:


 * 1) Make the group of option buttons a control array.
 * 2) In the Click event handler for the control array, save the index of the selected option button into a global variable.
 * 3) When you want to know which option button is selected, check the global variable.

An alternative method to check which option button was selected is to examine the Value property of each option button in a sequence of If-Then statements, or in a Select Case statement.

Step-by-Step Example
 Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default. Place a command button (Command1) and option button (Option1) on Form1. With Option1 selected, from the Edit menu, choose Copy. From the Edit menu, choose Paste. A dialog box asks you if you want to create a control array. Choose Yes. Change the Caption property of the two option buttons to &quot;option 1&quot; and &quot;option 2&quot;.  Enter the following code into the general Declarations section of Form1: Dim option_index As Integer   Enter the following code into the Option1 control array Click event procedure: Sub Option1_Click (Index As Integer) option_index = Index End Sub </li>  Enter the following code into the Command1 Click event procedure: Sub Command1_Click MsgBox Option1(option_index).Caption End Sub </li> Press F5 to run the program. When you click Command1, a dialog box displays the caption of the selected option button.</li></ol>

Additional query words: 2.00 3.00

Keywords: KB88910

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.