PRB: Click Event Invoked When Option Button Receives Focus

Last reviewed: February 21, 1996
Article ID: Q88792
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0

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

  1. 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.

  2. Set the AutoRedraw property for Form1 to True. This prevents any text printed to the screen from being overwritten when Windows redraws the form.

  3. Create one or more option buttons on Form1.

  4. In the Click event procedure for the first option button, insert the following code:

         PRINT "Option Button Clicked"
    

  5. From the Run menu, choose Start (ALT, R, S) to run the program.

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 reference words: 1.00 2.00 3.00
KBCategory: kbprg kbprb
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 21, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.