TCheckBox / TRadio
Boolean checkboxes and mutually-exclusive radio button groups
TCheckBox
Source: source/classes/checkbox.prg | Parent: TControl
TCheckBox wraps the standard Windows checkbox control. It binds to a logical variable -- .T. when checked, .F. when unchecked. Checkboxes are independent; any number can be checked simultaneously.
TCheckBox Properties
| Property | Type | Description |
|---|---|---|
bSetGet | Block | Data-binding block that reads/writes the logical variable |
cCaption | String | Text displayed beside the checkbox |
lChecked | Logical | Current checked state |
TCheckBox Methods
| Method | Description |
|---|---|
New( nRow, nCol, cCaption, bSetGet, oWnd, ... ) | Constructor. |
Click() | Toggle the check state and evaluate bSetGet. |
Check() | Set checked state to .T. |
UnCheck() | Set checked state to .F. |
SetCheck( lOnOff ) | Set checked state to the given value. |
lIsChecked() | Returns the current visual check state from the Windows API. |
Refresh() | Re-read the bound variable and update the visual state. |
TCheckBox Command
@ nRow, nCol CHECKBOX oChk VAR lVar ;
PROMPT "Label text" ;
[ OF oWnd ] ;
[ SIZE nWidth, nHeight ] ;
[ ON CHANGE bChange ] ;
[ WHEN bWhen ] ;
[ FONT oFont ]
TRadio and TRadMenu
Source: source/classes/radio.prg | Parent: TControl
TRadio represents a single radio button. Radio buttons are mutually exclusive within a group -- selecting one deselects all others. The group is managed by a TRadMenu object, which binds to a numeric variable indicating which option is selected (1-based).
Radio Group Architecture
graph TD
RADMENU["TRadMenu
nOption = 2
bSetGet: {|u| nChoice}"] RADMENU --> R1["TRadio 1
nPos = 1
'Standard'"] RADMENU --> R2["TRadio 2
nPos = 2
'Express' (selected)"] RADMENU --> R3["TRadio 3
nPos = 3
'Overnight'"] R2 -.->|"Click"| RADMENU RADMENU -.->|"Select(Self)"| R2 RADMENU -.->|"Deselect others"| R1 RADMENU -.->|"Deselect others"| R3 style RADMENU fill:#1c2129,stroke:#58a6ff,stroke-width:2px,color:#e6edf3 style R2 fill:#1c2129,stroke:#3fb950,stroke-width:2px,color:#e6edf3 style R1 fill:#1c2129,stroke:#30363d,stroke-width:1px,color:#8b949e style R3 fill:#1c2129,stroke:#30363d,stroke-width:1px,color:#8b949e
nOption = 2
bSetGet: {|u| nChoice}"] RADMENU --> R1["TRadio 1
nPos = 1
'Standard'"] RADMENU --> R2["TRadio 2
nPos = 2
'Express' (selected)"] RADMENU --> R3["TRadio 3
nPos = 3
'Overnight'"] R2 -.->|"Click"| RADMENU RADMENU -.->|"Select(Self)"| R2 RADMENU -.->|"Deselect others"| R1 RADMENU -.->|"Deselect others"| R3 style RADMENU fill:#1c2129,stroke:#58a6ff,stroke-width:2px,color:#e6edf3 style R2 fill:#1c2129,stroke:#3fb950,stroke-width:2px,color:#e6edf3 style R1 fill:#1c2129,stroke:#30363d,stroke-width:1px,color:#8b949e style R3 fill:#1c2129,stroke:#30363d,stroke-width:1px,color:#8b949e
TRadio Properties
| Property | Type | Description |
|---|---|---|
oRadMenu | Object | Reference to the TRadMenu that owns this radio button |
nPos | Numeric | Position index within the group. When this radio is selected, TRadMenu:nOption equals nPos. |
cCaption | String | Text displayed beside the radio button |
Radio Command
@ nRow, nCol RADIO oRad VAR nChoice ;
PROMPT "Option text" ;
[ OF oWnd ] ;
[ WHEN bWhen ] ;
[ ON CHANGE bChange ]
When multiple RADIO commands share the same VAR, FiveWin automatically groups them into a TRadMenu.
Checkbox vs. Radio: When to Use Which
graph LR
Q{"User needs to..."}
Q -->|"toggle on/off"| CB["Use TCheckBox
Independent, any combo OK"] Q -->|"choose 1 of N"| RB["Use TRadio group
Mutually exclusive"] Q -->|"select multiple from list"| ML["Use TCheckBox group
or TXBrowse with checkmarks"] style CB fill:#1c2129,stroke:#3fb950,color:#e6edf3 style RB fill:#1c2129,stroke:#58a6ff,color:#e6edf3 style ML fill:#1c2129,stroke:#d29922,color:#e6edf3
Independent, any combo OK"] Q -->|"choose 1 of N"| RB["Use TRadio group
Mutually exclusive"] Q -->|"select multiple from list"| ML["Use TCheckBox group
or TXBrowse with checkmarks"] style CB fill:#1c2129,stroke:#3fb950,color:#e6edf3 style RB fill:#1c2129,stroke:#58a6ff,color:#e6edf3 style ML fill:#1c2129,stroke:#d29922,color:#e6edf3
Code Examples
Example 1: Checkboxes for Options
#include "FiveWin.ch"
FUNCTION PrintOptions()
LOCAL oDlg
LOCAL lLandscape := .F.
LOCAL lColor := .T.
LOCAL lDuplex := .F.
LOCAL lCollate := .T.
LOCAL lPreview := .F.
DEFINE DIALOG oDlg TITLE "Print Options" SIZE 350, 320
@ 1.0, 2 SAY "Select print options:" OF oDlg
@ 2.5, 2 CHECKBOX lLandscape PROMPT "&Landscape orientation" OF oDlg SIZE 200, 18
@ 4.0, 2 CHECKBOX lColor PROMPT "&Color printing" OF oDlg SIZE 200, 18
@ 5.5, 2 CHECKBOX lDuplex PROMPT "&Duplex (two-sided)" OF oDlg SIZE 200, 18
@ 7.0, 2 CHECKBOX lCollate PROMPT "Co&llate copies" OF oDlg SIZE 200, 18
@ 8.5, 2 CHECKBOX lPreview PROMPT "Print Pre&view" OF oDlg SIZE 200, 18
@ 11, 5 BUTTON "&Print" OF oDlg SIZE 80, 25 ;
ACTION ( PrintDoc( lLandscape, lColor, lDuplex, lCollate, lPreview ), ;
oDlg:End() ) DEFAULT
@ 11, 15 BUTTON "&Cancel" OF oDlg SIZE 80, 25 ACTION oDlg:End() CANCEL
ACTIVATE DIALOG oDlg CENTERED
RETURN NIL
Example 2: Radio Button Group
#include "FiveWin.ch"
FUNCTION ShippingMethod()
LOCAL oDlg
LOCAL nShipping := 1 // 1=Standard, 2=Express, 3=Overnight
DEFINE DIALOG oDlg TITLE "Shipping Method" SIZE 350, 280
@ 1.0, 2 SAY "Select shipping method:" OF oDlg
@ 2.5, 3 RADIO nShipping PROMPT "&Standard (5-7 days)" OF oDlg
@ 4.0, 3 RADIO nShipping PROMPT "&Express (2-3 days)" OF oDlg
@ 5.5, 3 RADIO nShipping PROMPT "&Overnight (1 day)" OF oDlg
@ 8, 5 BUTTON "&OK" OF oDlg SIZE 80, 25 ;
ACTION ( ShowShippingCost( nShipping ), oDlg:End() ) DEFAULT
@ 8, 15 BUTTON "&Cancel" OF oDlg SIZE 80, 25 ACTION oDlg:End() CANCEL
ACTIVATE DIALOG oDlg CENTERED
RETURN NIL
STATIC FUNCTION ShowShippingCost( nMethod )
LOCAL aCosts := { 4.99, 12.99, 29.99 }
LOCAL aNames := { "Standard", "Express", "Overnight" }
MsgInfo( aNames[ nMethod ] + " shipping: $" + ;
LTrim( Str( aCosts[ nMethod ], 8, 2 ) ) )
RETURN NIL
Example 3: Mixed Checkboxes and Radios in a Settings Dialog
#include "FiveWin.ch"
FUNCTION AppSettings()
LOCAL oDlg, oFld, oDlg1, oDlg2
// Display settings
LOCAL lShowToolbar := .T.
LOCAL lShowStatus := .T.
LOCAL lFullScreen := .F.
LOCAL nTheme := 1 // 1=Light, 2=Dark, 3=System
// Behavior settings
LOCAL lAutoSave := .T.
LOCAL lConfirmDel := .T.
LOCAL lSounds := .F.
LOCAL nStartup := 1 // 1=Last file, 2=New file, 3=Nothing
DEFINE DIALOG oDlg TITLE "Application Settings" SIZE 450, 400
@ 0.3, 0.3 FOLDER oFld OF oDlg ;
PROMPTS "Display", "Behavior" ;
DIALOGS oDlg1, oDlg2 ;
SIZE 420, 280
// --- Display page ---
@ 1.0, 2 CHECKBOX lShowToolbar PROMPT "Show &Toolbar" OF oDlg1 SIZE 200, 18
@ 2.5, 2 CHECKBOX lShowStatus PROMPT "Show &Status Bar" OF oDlg1 SIZE 200, 18
@ 4.0, 2 CHECKBOX lFullScreen PROMPT "&Full Screen Mode" OF oDlg1 SIZE 200, 18
@ 6.0, 2 SAY "Color Theme:" OF oDlg1
@ 7.5, 3 RADIO nTheme PROMPT "&Light" OF oDlg1
@ 9.0, 3 RADIO nTheme PROMPT "&Dark" OF oDlg1
@ 10.5, 3 RADIO nTheme PROMPT "&System" OF oDlg1
// --- Behavior page ---
@ 1.0, 2 CHECKBOX lAutoSave PROMPT "&Auto-save every 5 minutes" OF oDlg2 SIZE 250, 18
@ 2.5, 2 CHECKBOX lConfirmDel PROMPT "&Confirm before deleting" OF oDlg2 SIZE 250, 18
@ 4.0, 2 CHECKBOX lSounds PROMPT "Enable &Sounds" OF oDlg2 SIZE 250, 18
@ 6.0, 2 SAY "On startup:" OF oDlg2
@ 7.5, 3 RADIO nStartup PROMPT "Open &last file" OF oDlg2
@ 9.0, 3 RADIO nStartup PROMPT "Create &new file" OF oDlg2
@ 10.5, 3 RADIO nStartup PROMPT "Do ¬hing" OF oDlg2
// Buttons on the parent dialog
@ 23, 8 BUTTON "&Save" OF oDlg SIZE 80, 25 ;
ACTION ( SaveSettings( lShowToolbar, lShowStatus, lFullScreen, nTheme, ;
lAutoSave, lConfirmDel, lSounds, nStartup ), ;
oDlg:End() ) DEFAULT
@ 23, 18 BUTTON "&Cancel" OF oDlg SIZE 80, 25 ACTION oDlg:End() CANCEL
ACTIVATE DIALOG oDlg CENTERED
RETURN NIL
Tips
- Checkbox binds to a logical variable. Radio group binds to a numeric variable (the option index).
- FiveWin auto-groups consecutive
RADIOcommands sharing the sameVARinto aTRadMenu. - Use
&in the PROMPT to set an Alt-key accelerator:"&Landscape"means Alt+L toggles it. - For a checkbox bound to a DBF logical field, use
FieldWBlock( "ACTIVE", Select() )as thebSetGet. - The
ON CHANGEclause fires after each state change, useful for enabling/disabling dependent controls. - Radio buttons support arrow-key navigation within the group automatically.
- For more than 5-6 options, consider using a
TComboBoxinstead of a long radio group.