|
| 1 | +# CheckboxControl |
| 2 | + |
| 3 | +Toggleable checkbox control with a label, checked/unchecked state, and keyboard/mouse support. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +CheckboxControl displays a label next to a `[ ]` / `[X]` indicator and toggles its checked state when activated. It responds to keyboard (Space/Enter) and mouse clicks, and fires a `CheckedChanged` event whenever the state flips. |
| 8 | + |
| 9 | +The checked and unchecked indicator characters are fully customizable, so the control can render anything from a classic `X` to a check mark (`✓`), a dot (`●`), or any other single visible character. Colors for the normal, focused, and disabled states, as well as the checkmark itself, can be overridden or left to inherit from the active theme. |
| 10 | + |
| 11 | +The control measures itself to a single row sized to ` [X] Label `, but it also honors a fixed `Width` and `HorizontalAlignment` (including `Stretch`) when laid out inside containers. |
| 12 | + |
| 13 | +See also: [ButtonControl](ButtonControl.md) |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +```csharp |
| 18 | +var checkbox = Controls.Checkbox("Enable notifications") |
| 19 | + .Checked(true) |
| 20 | + .OnCheckedChanged((sender, isChecked) => |
| 21 | + { |
| 22 | + // React to the new state |
| 23 | + }) |
| 24 | + .Build(); |
| 25 | + |
| 26 | +window.AddControl(checkbox); |
| 27 | +``` |
| 28 | + |
| 29 | +## Builder API |
| 30 | + |
| 31 | +### Content |
| 32 | + |
| 33 | +```csharp |
| 34 | +Controls.Checkbox("Label") // Factory sets the initial label |
| 35 | + .WithLabel("Enable feature") // Override the label text |
| 36 | + .Checked(true) // Set initial checked state (default: true when called) |
| 37 | + .Checked(false); // Explicitly start unchecked |
| 38 | +``` |
| 39 | + |
| 40 | +### Indicator Characters |
| 41 | + |
| 42 | +```csharp |
| 43 | +Controls.Checkbox("Custom marks") |
| 44 | + .WithCheckedCharacter("✓") // Character shown when checked (default "X") |
| 45 | + .WithUncheckedCharacter("·") // Character shown when unchecked (default " ") |
| 46 | + .WithCheckmark("✓", "·"); // Set both at once (unchecked defaults to " ") |
| 47 | +``` |
| 48 | + |
| 49 | +### Layout |
| 50 | + |
| 51 | +```csharp |
| 52 | +Controls.Checkbox("Label") |
| 53 | + .WithWidth(30) // Fixed width |
| 54 | + .WithAlignment(HorizontalAlignment.Center) // Horizontal alignment |
| 55 | + .WithVerticalAlignment(VerticalAlignment.Top) |
| 56 | + .WithMargin(1) // Uniform margin |
| 57 | + .WithMargin(1, 0, 1, 0) // left, top, right, bottom |
| 58 | + .Visible(true) |
| 59 | + .StickyTop() // Stick to top of window |
| 60 | + .StickyBottom() // Stick to bottom of window |
| 61 | + .WithStickyPosition(StickyPosition.None); |
| 62 | +``` |
| 63 | + |
| 64 | +### Identity |
| 65 | + |
| 66 | +```csharp |
| 67 | +Controls.Checkbox("Label") |
| 68 | + .WithName("myCheckbox") // Name for FindControl<CheckboxControl>("myCheckbox") |
| 69 | + .WithTag(myData); // Arbitrary tag object |
| 70 | +``` |
| 71 | + |
| 72 | +### Events |
| 73 | + |
| 74 | +```csharp |
| 75 | +Controls.Checkbox("Label") |
| 76 | + .OnCheckedChanged((sender, isChecked) => { /* ... */ }) |
| 77 | + .OnCheckedChanged((sender, isChecked, window) => { /* window-aware */ }) |
| 78 | + .OnGotFocus((sender, e) => { /* ... */ }) |
| 79 | + .OnGotFocus((sender, e, window) => { /* window-aware */ }) |
| 80 | + .OnLostFocus((sender, e) => { /* ... */ }) |
| 81 | + .OnLostFocus((sender, e, window) => { /* window-aware */ }); |
| 82 | +``` |
| 83 | + |
| 84 | +## Properties |
| 85 | + |
| 86 | +| Property | Type | Default | Description | |
| 87 | +|----------|------|---------|-------------| |
| 88 | +| `Checked` | `bool` | `false` | The checked state; setting it fires `CheckedChanged` | |
| 89 | +| `Label` | `string` | `"Checkbox"` | Text displayed next to the checkbox | |
| 90 | +| `CheckedCharacter` | `string` | `"X"` | Character shown inside the box when checked (empty/null falls back to default) | |
| 91 | +| `UncheckedCharacter` | `string` | `" "` | Character shown inside the box when unchecked (empty/null falls back to default) | |
| 92 | +| `IsEnabled` | `bool` | `true` | Whether the checkbox can be interacted with | |
| 93 | +| `Width` | `int?` | `null` | Fixed width (auto-sized to content if null) | |
| 94 | +| `BackgroundColor` | `Color?` | `null` | Background color in the normal state (uses theme if null) | |
| 95 | +| `ForegroundColor` | `Color` | theme `ButtonForegroundColor` / `Color.White` | Text color in the normal state | |
| 96 | +| `FocusedBackgroundColor` | `Color?` | `null` | Background color when focused (uses theme if null) | |
| 97 | +| `FocusedForegroundColor` | `Color` | theme `ButtonFocusedForegroundColor` / `Color.White` | Text color when focused | |
| 98 | +| `DisabledBackgroundColor` | `Color?` | `null` | Background color when disabled (uses theme if null) | |
| 99 | +| `DisabledForegroundColor` | `Color` | theme `ButtonDisabledForegroundColor` / `Color.DarkSlateGray1` | Text color when disabled | |
| 100 | +| `CheckmarkColor` | `Color` | theme `ButtonFocusedForegroundColor` / `Color.Cyan1` | Color of the checkmark character when checked | |
| 101 | +| `HasFocus` | `bool` | `false` | Whether the checkbox currently has keyboard focus | |
| 102 | +| `CanReceiveFocus` | `bool` | `true` | Whether the checkbox can receive focus (mirrors `IsEnabled`) | |
| 103 | +| `WantsMouseEvents` | `bool` | `true` | Whether the checkbox wants mouse events (mirrors `IsEnabled`) | |
| 104 | +| `CanFocusWithMouse` | `bool` | `true` | Whether a mouse click can focus the checkbox (mirrors `IsEnabled`) | |
| 105 | + |
| 106 | +## Events |
| 107 | + |
| 108 | +| Event | Arguments | Description | |
| 109 | +|-------|-----------|-------------| |
| 110 | +| `CheckedChanged` | `EventHandler<bool>` | Fired when the checked state changes; argument is the new state | |
| 111 | +| `CheckedChangedAsync` | `AsyncEventHandler<bool>` | Async counterpart of `CheckedChanged` | |
| 112 | +| `MouseClick` | `EventHandler<MouseEventArgs>` | Fired when the checkbox is left-clicked | |
| 113 | +| `MouseDoubleClick` | `EventHandler<MouseEventArgs>` | Fired when the checkbox is double-clicked | |
| 114 | +| `MouseRightClick` | `EventHandler<MouseEventArgs>` | Fired when the checkbox is right-clicked | |
| 115 | +| `MouseEnter` | `EventHandler<MouseEventArgs>` | Fired when the mouse enters the checkbox area | |
| 116 | +| `MouseLeave` | `EventHandler<MouseEventArgs>` | Fired when the mouse leaves the checkbox area | |
| 117 | +| `MouseMove` | `EventHandler<MouseEventArgs>` | Fired when the mouse moves over the checkbox | |
| 118 | +| `GotFocus` | `EventHandler` | Fired when the checkbox receives focus | |
| 119 | +| `LostFocus` | `EventHandler` | Fired when the checkbox loses focus | |
| 120 | + |
| 121 | +## Keyboard Support |
| 122 | + |
| 123 | +| Key | Action | |
| 124 | +|-----|--------| |
| 125 | +| **Space** | Toggle checked state | |
| 126 | +| **Enter** | Toggle checked state | |
| 127 | +| **Tab** | Move focus to next control | |
| 128 | +| **Shift+Tab** | Move focus to previous control | |
| 129 | + |
| 130 | +Keys are only processed while the checkbox is enabled and focused. |
| 131 | + |
| 132 | +## Mouse Support |
| 133 | + |
| 134 | +| Action | Result | |
| 135 | +|--------|--------| |
| 136 | +| **Left Click** | Toggle checked state and fire `MouseClick` | |
| 137 | +| **Double Click** | Fire `MouseDoubleClick` (does not toggle again) | |
| 138 | +| **Right Click** | Fire `MouseRightClick` | |
| 139 | +| **Click** | Give the checkbox keyboard focus | |
| 140 | +| **Enter / Leave** | Fire `MouseEnter` / `MouseLeave` | |
| 141 | + |
| 142 | +Mouse events are only processed when enabled, and clicks in the control's margin area are ignored. |
| 143 | + |
| 144 | +## Examples |
| 145 | + |
| 146 | +### Simple Checkbox |
| 147 | + |
| 148 | +```csharp |
| 149 | +window.AddControl( |
| 150 | + Controls.Checkbox("Enable notifications") |
| 151 | + .Checked(true) |
| 152 | + .Build() |
| 153 | +); |
| 154 | +``` |
| 155 | + |
| 156 | +### Reacting to State Changes |
| 157 | + |
| 158 | +```csharp |
| 159 | +window.AddControl( |
| 160 | + Controls.Checkbox("Auto-save on exit") |
| 161 | + .OnCheckedChanged((sender, isChecked) => |
| 162 | + { |
| 163 | + windowSystem.NotificationStateService.ShowNotification( |
| 164 | + "Auto-save", |
| 165 | + isChecked ? "Enabled" : "Disabled", |
| 166 | + NotificationSeverity.Info |
| 167 | + ); |
| 168 | + }) |
| 169 | + .Build() |
| 170 | +); |
| 171 | +``` |
| 172 | + |
| 173 | +### Custom Checkmark Characters |
| 174 | + |
| 175 | +```csharp |
| 176 | +window.AddControl( |
| 177 | + Controls.Checkbox("Custom marks") |
| 178 | + .WithCheckmark("✓", "·") |
| 179 | + .Checked(true) |
| 180 | + .Build() |
| 181 | +); |
| 182 | +``` |
| 183 | + |
| 184 | +### Reading State From Another Control |
| 185 | + |
| 186 | +```csharp |
| 187 | +window.AddControl( |
| 188 | + Controls.Checkbox("Animate background gradient") |
| 189 | + .WithName("animToggle") |
| 190 | + .Checked(true) |
| 191 | + .Build() |
| 192 | +); |
| 193 | + |
| 194 | +// Elsewhere (e.g. an async window thread): |
| 195 | +var toggle = window.FindControl<CheckboxControl>("animToggle"); |
| 196 | +if (toggle?.Checked == true) |
| 197 | +{ |
| 198 | + // run the animation |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +### Two-Way Data Binding |
| 203 | + |
| 204 | +```csharp |
| 205 | +var enabledCheckbox = Controls.Checkbox("Monitoring Enabled") |
| 206 | + .Build(); |
| 207 | + |
| 208 | +// Keep the view model and the checkbox in sync both ways |
| 209 | +enabledCheckbox.BindTwoWay(vm, v => v.MonitoringEnabled, c => c.Checked); |
| 210 | + |
| 211 | +window.AddControl(enabledCheckbox); |
| 212 | +``` |
| 213 | + |
| 214 | +### Settings Panel |
| 215 | + |
| 216 | +```csharp |
| 217 | +panel.AddControl(Controls.Checkbox("Enable notifications").Checked(true).Build()); |
| 218 | +panel.AddControl(Controls.Checkbox("Auto-save on exit").Checked(true).Build()); |
| 219 | +panel.AddControl(Controls.Checkbox("Show status bar").Build()); |
| 220 | +panel.AddControl(Controls.Checkbox("Enable telemetry").Build()); |
| 221 | +``` |
| 222 | + |
| 223 | +## Best Practices |
| 224 | + |
| 225 | +1. **Use clear labels**: The label should describe the option being toggled ("Enable notifications", not "Notifications?"). |
| 226 | +2. **Set the initial state explicitly**: Use `.Checked(true)` for options that should default to on so the UI matches your model. |
| 227 | +3. **Prefer `CheckedChanged` over polling**: React to state changes through the event rather than reading `Checked` on a timer. |
| 228 | +4. **Bind to your model**: Use `BindTwoWay` to keep a view-model property and the checkbox in sync without manual event wiring. |
| 229 | +5. **Name checkboxes you read later**: Use `.WithName(...)` so you can retrieve state with `FindControl<CheckboxControl>(...)`. |
| 230 | +6. **Customize marks for clarity**: Use `WithCheckmark` to match your visual style, but keep characters single-width for clean alignment. |
| 231 | + |
| 232 | +## See Also |
| 233 | + |
| 234 | +- [ButtonControl](ButtonControl.md) - For click actions |
| 235 | +- [PromptControl](PromptControl.md) - For text input |
| 236 | + |
| 237 | +--- |
| 238 | + |
| 239 | +[Back to Controls](../CONTROLS.md) | [Back to Main Documentation](../../README.md) |
0 commit comments