Win8ToggleSwitch
A flat, square-cornered on/off switch in the Windows 8 style: a framed track with a gradient interior and a solid bar – the stick – that sits at the left end while off and at the right end while on.
public class Win8ToggleSwitch : Control
Inherits from / implements: Control
Remarks
Use it for settings that take effect at once, where a check box would do but a switch reads
better – "Auto-save", "Show completed tasks", "Use proxy server". Read and set the state with
IsChecked and handle Toggled to act on it.
The parts and their colours
Every part has one colour for the on state and one for the off state, so the switch can say which state it is in by colour as well as by the stick's position. The two sets start out identical; set the on-state colours to something brighter to make the difference plain.
| Part | On / off colour properties |
|---|---|
Outer border – the frame round the track |
|
Inner border – the line inside the frame |
|
Track interior – a top-to-bottom gradient |
|
Stick – the full-height bar that moves |
|
ShouldDrawGapBetweenSwitchStickAndTrack cuts a slot between the stick and the
track, and ShouldDrawFocusRectangleWhenFocused shows a dotted rectangle while the switch
has the keyboard focus. All widths are in logical pixels and are scaled for the display.
Using it
A click on the switch toggles it, and so does Enter or Space while it has the focus. The switch
has no default size: give it a Control.Size, such as 60 by 24.
Examples
An "Auto-save" switch on a document editor's toolbar panel, green while on and grey while off.
using System;
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.ToggleControls;
public class EditorOptionsPanel : Panel
{
private readonly Win8ToggleSwitch _autoSave = new Win8ToggleSwitch();
public EditorOptionsPanel()
{
Size = new Size(260, 48);
var caption = new Label { Text = "Auto-save", Location = new Point(12, 16), AutoSize = true };
_autoSave.Location = new Point(120, 12);
_autoSave.Size = new Size(60, 24);
// On: a green stick on a light green track.
_autoSave.ToggleStickColor = Color.SeaGreen;
_autoSave.BackgroundStartColor = Color.Honeydew;
_autoSave.BackgroundEndColor = Color.LightGreen;
// Off: a grey stick on a grey track.
_autoSave.ToggleStickColorUnchecked = Color.DimGray;
_autoSave.BackgroundStartColorUnchecked = Color.White;
_autoSave.BackgroundEndColorUnchecked = Color.Gainsboro;
_autoSave.ShouldDrawFocusRectangleWhenFocused = true;
_autoSave.Toggled += (sender, e) => Console.WriteLine("Auto-save " + (_autoSave.IsChecked ? "on" : "off"));
Controls.Add(caption);
Controls.Add(_autoSave);
}
}See also
Members at a glance
| Constructors | |
|---|---|
Win8ToggleSwitch | Initialises a new switch in the off state, with an orange-red stick, a light grey frame and a white to dark grey track. |
| Properties | |
BackgroundEndColor | Gets or sets the colour of the bottom of the track's gradient interior while the switch is on. |
BackgroundEndColorUnchecked | Gets or sets the colour of the bottom of the track's gradient interior while the switch is off. |
BackgroundStartColor | Gets or sets the colour of the top of the track's gradient interior while the switch is on. |
BackgroundStartColorUnchecked | Gets or sets the colour of the top of the track's gradient interior while the switch is off. |
InnerBorderColor | Gets or sets the colour of the line just inside the outer frame while the switch is on. |
InnerBorderColorUnchecked | Gets or sets the colour of the line just inside the outer frame while the switch is off. |
InnerBorderThickness | Gets or sets the width of the line drawn just inside the outer frame. |
IsChecked | Gets or sets whether the switch is on. |
OuterBorderColor | Gets or sets the colour of the outer frame round the track while the switch is on. |
OuterBorderColorUnchecked | Gets or sets the colour of the outer frame round the track while the switch is off. |
OuterBorderThickness | Gets or sets the width of the outer frame round the track. |
ShouldDrawFocusRectangleWhenFocused | Gets or sets whether a dotted focus rectangle is drawn round the switch while it has the keyboard focus. |
ShouldDrawGapBetweenSwitchStickAndTrack | Gets or sets whether a slot is cut between the stick and the track. |
SwitchStickControlGapWidth | Gets or sets the width of the slot between the stick and the track. |
ToggleStickColor | Gets or sets the colour of the stick while the switch is on. |
ToggleStickColorUnchecked | Gets or sets the colour of the stick while the switch is off. |
ToggleSwitchStickWidth | Gets or sets the width of the stick – the full-height bar that marks the switch's state. |
| Methods | |
Dispose | Releases the switch's drawing surface and the resources of the underlying control. |
OnGotFocus | Raises the Control.GotFocus event and repaints the switch. |
OnHandleCreated | Raises the Control.HandleCreated event and prepares the switch's drawing surface. |
OnKeyDown | Raises the Control.KeyDown event, and repaints when the focus arrives by the Tab key so
the focus rectangle appears. |
OnKeyUp | Raises the Control.KeyUp event, then toggles IsChecked when the key
released is Enter or Space and the switch has the focus. |
OnLostFocus | Raises the Control.LostFocus event and repaints the switch. |
OnMouseEnter | Raises the Control.MouseEnter event and repaints the switch. |
OnMouseLeave | Raises the Control.MouseLeave event and repaints the switch. |
OnMouseUp | Raises the Control.MouseUp event, then toggles IsChecked. |
OnPaint | Raises the Control.Paint event, then draws the frame, track, stick and focus rectangle
in the colours for the current state. |
OnParentChanged | Raises the Control.ParentChanged event and prepares the switch's drawing surface. |
| Events | |
Toggled | Occurs when IsChecked is set – by a click, by Enter or Space, or from code. |
Constructors
Win8ToggleSwitch
Initialises a new switch in the off state, with an orange-red stick, a light grey frame and a white to dark grey track.
Remarks
The switch starts with an empty Control.Size; set one before showing it.
Properties
BackgroundEndColor
Gets or sets the colour of the bottom of the track's gradient interior while the switch is on.
- Value
- The default is
Color.DarkGray.
See also
BackgroundEndColorUnchecked
Gets or sets the colour of the bottom of the track's gradient interior while the switch is off.
- Value
- The default is
Color.DarkGray.
See also
BackgroundStartColor
Gets or sets the colour of the top of the track's gradient interior while the switch is on.
- Value
- The default is
Color.White.
See also
BackgroundStartColorUnchecked
Gets or sets the colour of the top of the track's gradient interior while the switch is off.
- Value
- The default is
Color.White.
See also
InnerBorderColor
Gets or sets the colour of the line just inside the outer frame while the switch is on.
- Value
- The default is
Color.Black.
See also
InnerBorderColorUnchecked
Gets or sets the colour of the line just inside the outer frame while the switch is off.
- Value
- The default is
Color.Black.
See also
InnerBorderThickness
Gets or sets the width of the line drawn just inside the outer frame.
- Value
- The line width in logical pixels. The default is 1.
See also
IsChecked
Gets or sets whether the switch is on.
- Value
truewhen the switch is on (stick at the right end);falsewhen it is off (stick at the left end). The default isfalse.
Remarks
Setting it raises Toggled – every time, even when the value is unchanged – and
repaints the switch straight away in the colours for the new state.
OuterBorderColor
Gets or sets the colour of the outer frame round the track while the switch is on.
- Value
- The default is
Color.LightGray.
See also
OuterBorderColorUnchecked
Gets or sets the colour of the outer frame round the track while the switch is off.
- Value
- The default is
Color.LightGray.
See also
OuterBorderThickness
Gets or sets the width of the outer frame round the track.
- Value
- The frame width in logical pixels. The default is 3.
Remarks
The frame is drawn in OuterBorderColor or OuterBorderColorUnchecked.
Borders too thick for the switch's size leave no room for the track, and the switch is then not
painted at all – keep the two border widths well under half the switch's height.
See also
ShouldDrawFocusRectangleWhenFocused
Gets or sets whether a dotted focus rectangle is drawn round the switch while it has the keyboard focus.
- Value
trueto show the focus rectangle; the default isfalse.
Remarks
Turn it on where people move through the form with the Tab key, so they can see which switch Enter or Space will toggle.
ShouldDrawGapBetweenSwitchStickAndTrack
Gets or sets whether a slot is cut between the stick and the track.
- Value
trueto draw the slot,SwitchStickControlGapWidthwide; the default isfalse.
Remarks
The slot is painted in the background colour of the switch's parent (or the switch's own
Control.BackColor before it has a parent), so it looks like a gap through which the form
shows. It has no visible width until SwitchStickControlGapWidth is set above zero.
SwitchStickControlGapWidth
Gets or sets the width of the slot between the stick and the track.
- Value
- The slot width in logical pixels. The default is 0.
Remarks
Drawn only while ShouldDrawGapBetweenSwitchStickAndTrack is true.
A value below zero, or above half the switch's current width, is ignored and the old value kept.
Because the test uses the width at the moment the value is set, set Control.Size first –
on a switch that has not been sized yet, every value above zero is ignored.
Example
darkModeSwitch.Size = new Size(60, 24);
darkModeSwitch.SwitchStickControlGapWidth = 2;
darkModeSwitch.ShouldDrawGapBetweenSwitchStickAndTrack = true;ToggleStickColor
Gets or sets the colour of the stick while the switch is on.
- Value
- The default is
Color.OrangeRed.
See also
ToggleStickColorUnchecked
Gets or sets the colour of the stick while the switch is off.
- Value
- The default is
Color.OrangeRed.
See also
ToggleSwitchStickWidth
Gets or sets the width of the stick – the full-height bar that marks the switch's state.
- Value
- The stick width in logical pixels. The default is 13.
Methods
Dispose
Releases the switch's drawing surface and the resources of the underlying control.
Parameters
disposingtruewhen called fromDispose();falsewhen called from a finaliser.
OnGotFocus
Raises the Control.GotFocus event and repaints the switch.
Parameters
e- The event data.
OnHandleCreated
Raises the Control.HandleCreated event and prepares the switch's drawing surface.
Parameters
e- The event data.
OnKeyDown
Raises the Control.KeyDown event, and repaints when the focus arrives by the Tab key so
the focus rectangle appears.
Parameters
e- The key that was pressed.
OnKeyUp
Raises the Control.KeyUp event, then toggles IsChecked when the key
released is Enter or Space and the switch has the focus.
Parameters
e- The key that was released.
OnLostFocus
Raises the Control.LostFocus event and repaints the switch.
Parameters
e- The event data.
OnMouseEnter
Raises the Control.MouseEnter event and repaints the switch.
Parameters
e- The event data.
OnMouseLeave
Raises the Control.MouseLeave event and repaints the switch.
Parameters
e- The event data.
OnMouseUp
Raises the Control.MouseUp event, then toggles IsChecked.
Parameters
e- The mouse button and position.
Remarks
Any mouse button toggles the switch, the right button included.
OnPaint
Raises the Control.Paint event, then draws the frame, track, stick and focus rectangle
in the colours for the current state.
Parameters
pe- The graphics surface and clip rectangle to paint.
OnParentChanged
Raises the Control.ParentChanged event and prepares the switch's drawing surface.
Parameters
e- The event data.
Events
Toggled
Occurs when IsChecked is set – by a click, by Enter or Space, or from code.
Remarks
The event is raised every time the property is set, including when it is set to the value it already
has, and before the switch is repainted. IsChecked already holds the new state.
Example
proxySwitch.Toggled += (sender, e) =>
{
proxyAddressTextBox.Enabled = proxySwitch.IsChecked;
};