How to Add Buttons to an OptionGroup Dynamically (147726)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0

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.

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.

Modification Type:MajorLast Reviewed:2/8/2000
Keywords:kbcode KB147726