Microsoft KB Archive/147726

= How to Add Buttons to an OptionGroup Dynamically =

Article ID: 147726

Article Last Modified on 2/8/2000

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition

-



This article was previously published under Q147726



SUMMARY
By using the Visual FoxPro class designer, you can create an option group class that will allow you to add multiple buttons to an option group. You can carry forward various properties of the original class by creating a method that will take care of adding and aligning the buttons for you. This article shows by example how to do it.



Step-by-Step Example
The following steps create a generic horizontal optiongroup class that can be added to a form. It has an addbuttons method that allows you to add additional buttons to the form at run time.

 On the File menu, click New, and choose a new Class. Create a new class called NewOption based on the OptionGroup class. In the Class Designer, select and delete the second option button (Option2). On the Class menu, click New Method, and create a new method called addbuttons. From the Properties sheet, set the object to be the optiongroup NewOption, click the Methods tab, and double-click the addbuttons method.  Add the following code to the addbuttons method to take care of adding the buttons, renaming them, and positioning them horizontally: ** AddButtons Method PARAMETERS nButton_Number * This parameter is the total number of buttons for the option group Thisform.lockscreen=.T.  This.ButtonCount = nButton_Number FOR i = 2 TO This.ButtonCount * Position it directly to the right of the previous one This.Buttons(i).Left = This.Buttons(i-1).Left +; This.Buttons(i-1).Width

* Compute its caption length= LEN(This.Buttons(1).Caption) cStart_Title=left(This.Buttons(1).Caption, length-1) This.Buttons(i).Caption = cStart_Title + alltrim(Str(i)) This.Buttons(i).Visible = .T.       This.Buttons(i).Top = This.Buttons(1).Top NEXT i  This.AutoSize=.T.   Thisform.Lockscreen=.F.                         Close and save this class, and then add it to a form.</li>  Place a command button (Command1) on the form, and put the following code in the button's Click event code: Thisform.NewOption1.addbuttons(4) </li> Run the form, and click Command1. You will see that four buttons now exist, that they have been spaced out horizontally, and that their captions have been propagated from the first button.</li></ol>

Additional query words: VFoxWin

Keywords: kbcode KB147726

-

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

© Microsoft Corporation. All rights reserved.