Microsoft KB Archive/131838

= How OptionGroup Buttons Behave =

Article ID: 131838

Article Last Modified on 8/25/1999

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition

-



This article was previously published under Q131838



SUMMARY
This article describes how OptionGroup buttons function.



Determining Which Option Button Is Clicked in an Option Group
With FoxPro version 2.x, you could determine which radio button was clicked by setting up a case structure to return the number of the clicked radio button.

In Visual FoxPro, radio buttons are called OptionGroup buttons, and in most cases, you no longer need a case structure to determine which option was clicked. Each OptionGroup button in the group can respond to a click event independently.

Step-by-Step Example
 Create a new form in Visual FoxPro. Using the OptionGroup tool, place a set (two is the default) of OptionGroup buttons on the form.  Double-click the OptionGroup object to bring up the code editor, and click Option1 so that you can edit the code for the events associated with the option button. Select the Click procedure. In the code editing window, type: Wait window "option button 1"   On the Object menu, click Option2. Then select the Click procedure. In the code editing window, type: Wait window "option button 2"  Run the form, and click the different option buttons. You should see the different wait windows activate with the appropriate text for each button.

Calling Parent Code
It is possible to encapsulate code even further by using the concept of an parent object. Option buttons are part of an OptionGroup class, so the OptionGroup object is called the parent or the container of the buttons. Messages can be sent to a parent object by prefacing the method call with this.parent.

In the following example, a set of OptionGroup buttons send messages to the Click method of the OptionGroup container (parent object):

 Create a new form. Place an OptionGroup object on the form, and change the button count property to 3.</li>  In each option button's Click procedure, place this code: this.parent.click </li> Change the Caption and Name property of the three buttons to Red, Green, and Blue.</li>  Place the following code in the Click procedure of the OptionGroup object itself: do case case this.value='red' thisform.backcolor=rgb(255,0,0) case this.value='green' thisform.backcolor=rgb(0,255,0) case this.value='blue' thisform.backcolor=rgb(0,255,0) endcase </li> Set the Value property of the OptionGroup object to "" (an empty string), so it is created (instantiated) with a character data type.</li> Run the form.</li> Click the different option buttons, and observe the behaviors.</li></ol>

The individual buttons call the parent object's Click procedure. The parent object in this case is the OptionGroup container. This code checks the Value property and determines, by using the case structure, which values to use when setting the form's background color.

Calling Button Code from the Parent
To run a given button's code from the OptionGroup object, refer to the event for that particular button. For example, the Click event of the OptionGroup object could run Option1 button's code with this code: this.option1.click This causes the code in the Click procedure of the Option1 button to run whenever the user clicks the option group frame instead of an actual button. Of course, the code in the Option1 Click procedure also runs when the user clicks the Option1 button directly. But even if another button had the focus, and the user clicks within the frame of the OptionGroup container, the code for the Option1 button is run.

Additional query words: VFoxWin

Keywords: KB131838

-

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

© Microsoft Corporation. All rights reserved.