Setting Option Button Properties

To manually adjust individual elements of an option button or command button group in the Form Designer, choose Edit from the group's shortcut menu.

You can set properties on individual buttons in the Properties window. You also can set these properties at run time by specifying the name of the option button and the desired property setting. For example, the following line of code, included in the method or event code of some object on the same form as the option button group, sets the caption of optCust in the option button group opgChoices:

THISFORM.opgChoices.optCust.Caption = "Sort by Customer"

You also can set these properties at run time by using the Buttons property and specifying the index number of the option button in the group. For example, if optCust is the third button in the group, the following line of code also sets the caption of optCust:

THISFORM.opgChoices.Buttons(3).Caption = "Sort by Customer"

To set properties on all buttons in a group

  • Use the SetAll method of the group.

    For example, the following line of code disables all the buttons in an option button group named opgMyGroup on a form:

    THISFORM.opgMyGroup.SetAll("Enabled",.F., "OptionButton")
    

Enabling and Disabling Buttons in a Group

The previous example shows how to programmatically disable all option buttons in a group. When the buttons are disabled, they are displayed in the colors specified in the DisabledForeColor and DisabledBackColor properties of the option buttons. You could also set the Enabled property of the option button group to false (.F.) to disable the group; however, there would be no visual clue for the user.

Determining Which Option Button Is Currently Selected

You can use the Value property of the option button group to determine which option button in the group is selected. If the control source for the button is numeric, you have five option buttons in a group. If the third button is selected, the Value property of the option button group is 3; if no option buttons are selected, the Value property of the option button group is 0.

You also can determine the caption of the selected option button by using the Value and Buttons properties of the group. For example, the following line of code stores the Caption property of the selected option button to a variable cSelected.

oGroup = THISFORM.opg1
cSelected = oGroup.Buttons(oGroup.Value).Caption

Filtering Lists with Option Buttons

If you have a small set of predetermined table filters, you could use option buttons to make it possible for users to switch between the filters.

The following example assumes a form with a list box (lstCustomers) and an option button group that contains three option buttons.

Property Settings for the List Box

Object Property Setting
lstCustomers RowSourceType 2 - Alias
lstCustomers RowSource Customer

The filters are set in the Click event code of the option buttons.

Event Code for Filtering a List when Users Choose an Option Button

Object Event Code
optAll Click
SET FILTER TO
GO TOP
THISFORM.lstCustomers.Requery
optCanada Click
SET FILTER TO customer.country = "Canada"
GO TOP
THISFORM.lstCustomers.Requery
optUK Click
SET FILTER TO customer.country = "UK"
GO TOP
THISFORM.lstCustomers.Requery

When the user closes the form, do not forget to reset the filter by including SET FILTER TO in the Click event of the closing button or in the Destroy event.

Tip   To refresh a list when the list source might have changed, use the Requery method.

See Also

Setting the Number of Option Buttons in an Option Button Group | Storing User Choices to a Table Using Option Buttons | Using Controls | Controls and Objects | Form Designer