Table of Contents

Switch Guide

TTMSFNCSwitch is a toggle control that alternates between an on (ssOn) and off (ssOff) state. It supports an animated sliding thumb, horizontal and vertical orientations, rounded corners, and two layout modes. In sloExtended layout the on/off text labels are visible alongside the thumb.

TTMSFNCSwitch states

Basic usage

Set the initial State and handle OnStateChange to respond to user interaction.

// Basic on/off switch; read state with Checked or State
TMSFNCSwitch1.State := ssOff;

// React to the user toggling the switch
procedure TForm1.TMSFNCSwitch1StateChange(Sender: TObject;
  ASwitchState: TTMSFNCCustomSwitchState);
begin
  if ASwitchState = ssOn then
    EnableFeature
  else
    DisableFeature;
end;

Checked is a public Boolean alias for State: True maps to ssOn, False to ssOff. Use either depending on context.

Appearance customisation

The AppearanceOn and AppearanceOff sub-objects style the track background in each state. ButtonAppearance styles the sliding thumb. Enable sloExtended to show on/off text.

// Extended layout with On/Off text labels and custom colours
TMSFNCSwitch1.Layout  := sloExtended;
TMSFNCSwitch1.Rounded := True;

TMSFNCSwitch1.AppearanceOn.Text := 'ON';
TMSFNCSwitch1.AppearanceOn.Fill.Color := $FF00AA44;
TMSFNCSwitch1.AppearanceOn.Fill.Kind  := gfkSolid;

TMSFNCSwitch1.AppearanceOff.Text := 'OFF';
TMSFNCSwitch1.AppearanceOff.Fill.Color := $FFB0B0B0;
TMSFNCSwitch1.AppearanceOff.Fill.Kind  := gfkSolid;

// Thumb button margin and fill
TMSFNCSwitch1.ButtonAppearance.Margin := 2;
TMSFNCSwitch1.ButtonAppearance.Fill.Color := gcWhite;

AnimationFactor (default 15) controls how quickly the thumb slides; lower values produce a faster animation.

Combining orientation, state persistence, and theme switching

The switch fits naturally in settings screens where the state should be saved and restored across sessions.

// Vertical switch used as a feature toggle that also updates a label
// and saves the preference when changed
TMSFNCSwitch1.Orientation := soVertical;
TMSFNCSwitch1.Layout      := sloSimple;
TMSFNCSwitch1.Rounded     := True;

// Restore persisted state on startup
TMSFNCSwitch1.State := TTMSFNCCustomSwitchState(AppSettings.DarkModeEnabled);

procedure TForm1.TMSFNCSwitch1StateChange(Sender: TObject;
  ASwitchState: TTMSFNCCustomSwitchState);
begin
  LabelMode.Caption := IfThen(ASwitchState = ssOn, 'Dark mode', 'Light mode');
  AppSettings.DarkModeEnabled := (ASwitchState = ssOn);
  ApplyTheme(ASwitchState = ssOn);
end;