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

DATATypeDescription
nBorderSizeNumericBorder width of the switch track
nThumbSizeNumericSize of the sliding thumb
nClrThumbNumericColor of the thumb indicator
nClrBorderNumericColor of the border around the track
lHoverLogicalMouse hover state flag
lRadioStyleLogicalRender as radio-style circle switch
lOvalStyleLogicalRender as oval-shaped toggle
lReadOnlyLogicalPrevent user interaction

Methods

MethodDescription
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:

ModeTriggerDescription
Standard toggleWidth larger than heightRectangular track with sliding thumb (default)
OvallOvalStyle := .T.Rounded/pill-shaped track with thumb
Radio-stylelRadStyle := .T.Circular on/off indicator
Check-boxWidth equals heightSquare 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

See Also