๐Ÿ”น Kt-Switch

Overview

KtSwitch is a modern, customizable toggle switch control that provides an intuitive way to switch between on/off states for settings and features. With smooth animations, flexible styling options, and distinct visual states, it offers a sleek alternative to traditional checkboxes for binary choices in Windows Forms applications.


Key Properties

Property
Type
Default
Description

Checked

bool

false

Gets or sets whether the switch is in the ON position

Value

bool

false

Alternative property for checked state

Bg

KtColor

PRIMARY

Background color of the switch track

Thumb

KtColor

Empty

Color of the sliding thumb/circle

StateOn

KtSwitchState

-

Visual properties when switch is ON

StateOff

KtSwitchState

-

Visual properties when switch is OFF

Animation

int

5

Speed of the sliding animation

ThumbMargin

int

5

Padding/margin around the thumb circle

BorderRadius

int?

null

Corner radius of the switch track

BorderRadiusThumb

int?

null

Corner radius of the thumb circle

BorderThickness

int?

2

Thickness of the border


State Properties (KtSwitchState)

Each state object (StateOn/StateOff) has these properties:

Property
Type
Description

Bg

KtColor

Background color of the switch track

Thumb

KtColor

Color of the sliding thumb

Border

KtColor

Border color of the switch


Events

Event
Arguments
Description

CheckedChanged

CheckedChangedEventArgs

Fires when the switch state changes

OnValuechange

EventArgs

Alternative event for value changes


Basic Usage

Simple Switch

Switch with Label


Styling Examples

Custom Colors

Using State Properties

iOS-Style Switch

Material Design Style

Minimal Flat Style


Common Patterns

Settings Panel

Feature Toggles

Privacy Settings

Form with Multiple Switches


Advanced Features

Custom Animation Speed

Custom Thumb Margin

Programmatic Control

State-Based Styling


Layout Patterns

TableLayoutPanel Integration

GroupBox with Switches


Validation and State Management


Conditional Logic


Design Tips

  1. Size Guidelines: Standard switch size is 60x30, but can be adjusted proportionally

  2. Animation Speed: Use 3-8 for smooth animations; higher values are faster

  3. Thumb Margin: Typically 3-5px for proper visual spacing

  4. Color Contrast: Ensure ON and OFF states are clearly distinguishable

  5. Border Radius: Set to half the height for pill-shaped switches

  6. State Consistency: Use consistent colors across similar switches

  7. Label Placement: Place labels to the left or right, maintaining alignment

  8. Disabled State: The control automatically adjusts opacity when disabled


Accessibility Features

  • Automatic cursor change to hand pointer

  • Click anywhere on the switch to toggle

  • Smooth animation provides visual feedback

  • Clear visual distinction between ON/OFF states

  • Disabled state is visually apparent

  • Keyboard accessibility through standard focus/click patterns


Performance Notes

  • Animation uses a timer for smooth transitions

  • Thumb position calculated dynamically on resize

  • Graphics rendered using GDI+ for quality

  • Control automatically cleans up resources on disposal


Size Recommendations

Use Case
Recommended Size
Thumb Margin

Standard

60x30

5

Compact

50x25

3

Large

70x35

6

iOS Style

55x30

3

Material

50x25

2


Migration Notes

If migrating from CheckBox:


Best Practices

  1. Use for Binary Choices: Switches work best for settings that take effect immediately

  2. Provide Clear Labels: Always accompany switches with descriptive labels

  3. Group Related Switches: Organize related toggles in panels or groups

  4. Feedback on Change: Show confirmation or apply changes immediately

  5. Disabled State: Use when options are unavailable but should remain visible

  6. Consistent Positioning: Align switches vertically or use consistent spacing

  7. State Persistence: Save switch states to user preferences

  8. Visual Hierarchy: Use size and color to indicate importance

Last updated

Was this helpful?