PRB: Click Event Invoked When Option Button Receives Focus

ID: Q88792

1.00 2.00 3.00 WINDOWS kbprg kbprb

The information in this article applies to:

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

Keywords          : PrgCtrlsStd 
Version           : 1.00 2.00 3.00
Platform          : WINDOWS

Last Reviewed: February 22, 1996