How to Determine Selected Control from Array of Option ButtonsLast reviewed: March 1, 1996Article ID: Q147673 |
The information in this article applies to:
SUMMARYA common programming task in Visual Basic, given a control array of option buttons, is to determine which option button in the array is selected. The option button that is selected will have its Value property set to True, while all others will have Value properties of False. This article outlines a short function that will return the index of the selected option button when it is passed a reference to a control array of option buttons. It will work correctly even some indexes are skipped or not present in the control array. In addition, to avoid logic errors in writing code, it will fail and return a -1 if it is passed a control array of another type of object (like a check box). The function was written by Bruce McKinney, author of Hardcore Visual Basic, which is published by Microsoft Press.
MORE INFORMATIONThe code for the function is: Function GetOption(opts As Object) As Integer On Error GoTo GetOptionFail Dim opt As OptionButton For Each opt In opts If opt.Value Then GetOption = opt.Index Exit Function End If NextGetOptionFail: GetOption = -1End Function This function can be called like this:
myVariable = GetOption(optMyButtons) 'Control syntax OKmyVariable = GetOption(optMyButtons()) 'Array syntax OK
|
Additional reference words: 4.00 vb4win vb4all
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |