How to Add Buttons to an OptionGroup Dynamically

Last reviewed: March 1, 1996
Article ID: Q147726
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0

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.

MORE INFORMATION

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.

  1. On the File menu, click New, and choose a new Class.

  2. Create a new class called NewOption based on the OptionGroup class.

  3. In the Class Designer, select and delete the second option button (Option2).

  4. On the Class menu, click New Method, and create a new method called addbuttons.

  5. From the Properties sheet, set the object to be the optiongroup NewOption, click the Methods tab, and double-click the addbuttons method.

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

  7. Close and save this class, and then add it to a form.

  8. Place a command button (Command1) on the form, and put the following code in the button's Click event code:

    Thisform.NewOption1.addbuttons(4)

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


Additional reference words: 3.00 VFoxWin
KBCategory: kbtool kbhowto kbcode
KBSubcategory: FxtoolFormdes


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: March 1, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.