TSwitch
Source: source/classes/tswitch.prg
Inherits from: TControl
TSwitch is a toggle switch control with four rendering modes: standard toggle, oval, radio-style, and check-box. It provides a modern on/off switch with customizable colors, thumb size, and border appearance. The control supports mouse hover effects, keyboard navigation, and read-only mode.
Command Syntax
@ nRow, nCol SWITCH oSw VAR lVar SIZE nW, nH PIXEL OF oWnd ;
COLORS nClrOn, nClrOff BORDER nClrBorder
Key DATA Members
| DATA | Type | Description |
|---|---|---|
nBorderSize | Numeric | Border width of the switch track |
nThumbSize | Numeric | Size of the sliding thumb |
nClrThumb | Numeric | Color of the thumb indicator |
nClrBorder | Numeric | Color of the border around the track |
lHover | Logical | Mouse hover state flag |
lRadioStyle | Logical | Render as radio-style circle switch |
lOvalStyle | Logical | Render as oval-shaped toggle |
lReadOnly | Logical | Prevent user interaction |
Methods
| Method | Description |
|---|---|
New( nRow, nCol, cCaption, bSetGet, oWnd, nWidth, nHeight, nHelpTopic, bChange, oFont, bValid, nClrFore, nClrBack, lDesign, lPixel, cMsg, lUpdate, bWhen, nBorderSize, nClrBorder, nThumbSize, nClrThumb, lRadStyle, lOval ) | Create a new TSwitch control |
ReDefine( nId, oWnd, bSetGet, bChange, nClrFore, nClrBack, lUpdate, nHelpId, cMsg, bWhen, bValid, lRadStyle, lOval ) | Redefine from dialog resource |
Toggle() | Toggle the switch state (on to off, off to on) |
SetCheck( lOnOff ) | Set the switch to a specific state (.T. = on, .F. = off) |
Check() | Set the switch to the on position |
UnCheck() | Set the switch to the off position |
Change() | Execute the bChange code block with the current value |
Click() | Handle mouse click (toggles the switch) |
Default() | Apply default initialization settings |
Rendering Modes
TSwitch supports four visual rendering modes determined by the aspect ratio and style flags:
| Mode | Trigger | Description |
|---|---|---|
| Standard toggle | Width larger than height | Rectangular track with sliding thumb (default) |
| Oval | lOvalStyle := .T. | Rounded/pill-shaped track with thumb |
| Radio-style | lRadStyle := .T. | Circular on/off indicator |
| Check-box | Width equals height | Square check-box style render |
Example: Toggle with Colors
#include "FiveWin.ch"
function Main()
local oWnd, oSw, lOn := .F.
DEFINE WINDOW oWnd TITLE "Toggle Switch Demo" SIZE 300, 200
@ 40, 30 SWITCH oSw VAR lOn SIZE 60, 22 PIXEL OF oWnd ;
COLORS CLR_GREEN, CLR_GRAY BORDER CLR_HGRAY
oSw:bChange := { | lValue | MsgInfo( If( lValue, "ON", "OFF" ), "Switch Changed" ) }
@ 120, 30 BUTTON "Toggle" SIZE 60, 25 PIXEL OF oWnd ;
ACTION oSw:Toggle()
ACTIVATE WINDOW oWnd CENTERED
return nil
Notes
- TSwitch uses
bSetGetto read and write its logical state. TheVARclause in theSWITCHcommand creates this code block automatically. - The
COLORSclause in the command syntax expects two colors: the first for the ON state track and the second for the OFF state track. - When
lReadOnlyis set to.T., mouse clicks and keyboard input are ignored but the switch can still be changed programmatically viaSetCheck(),Check(), orUnCheck(). - Mouse hover effects are enabled by default. The track and thumb change appearance when the mouse enters the control area.
- The control respects tab-stop order and responds to the Space key for toggling when focused.