Microsoft KB Archive/88792

= PRB: Click Event Invoked When Option Button Receives Focus =

Article ID: 88792

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 Q88792



SYMPTOMS
When an option button or group of option buttons is initially placed on a form in Microsoft Visual Basic for Windows, the buttons will remain unselected until the focus is shifted to one of the option buttons. This can cause unexpected results, because shifting the focus to one of the option buttons will invoke a Click event for that option button.



WORKAROUND
Use either the following methods to work around this feature:

In design mode, you must manually set the Value property for one of the option buttons to True.

-or-

If you have more controls on the form, you can set one of the controls with a tabindex property to 0. Therefore, when switching between forms, the focus will not automatically go to the first button.



STATUS
This behavior is by design.



MORE INFORMATION
In a group of option buttons, one of the buttons should be selected unless the option buttons apply to only certain selected objects on the current form. In this case, the option buttons, when initially displayed, would not be selected.

Visual Basic for Windows will allow option buttons to be placed on a form without selecting any of the option buttons. If you desire to create a group of option buttons with none of them selected, there is no way to prevent a Click event from being invoked when the focus is shifted to one of them. A problem may occur when an unselected option button is first in the tab order. The option button will automatically get selected when the form is shown. In all cases, to prevent the Click event from occurring, in design mode, you must set the Value property of one of the option buttons to True.

The following steps demonstrate this feature, as well as a way to work around this feature:

Steps to Reproduce Behavior
 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. Set the AutoRedraw property for Form1 to True. This prevents any text printed to the screen from being overwritten when Windows redraws the form. Create one or more option buttons on Form1.  In the Click event procedure for the first option button, insert the following code: PRINT &quot;Option Button Clicked&quot;  From the Run menu, choose Start (ALT, R, S) to run the program.</ol>

NOTE: The Click event is invoked when the form is shown and the focus is given to the first option button. To work around this problem, after creating the option buttons in step 2, set the Value property of one of the option buttons to True.

Additional query words: 2.00 3.00

Keywords: kbprb KB88792

-

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

© Microsoft Corporation. All rights reserved.