The TSwitch component is available in Delphi 10.4 Sydney and later versions. It is a toggle switch control that can be used to switch between two states (on/off, true/false, etc.).
Here are the steps to use the TSwitch component in Delphi:
- Add the TSwitch component to your form by dragging it from the Component Palette onto the form in the Designer.
- Set the properties of the TSwitch component as needed. For example, you can set the OnText and OffText properties to specify the text displayed when the switch is in the on or off state.
- You can use the IsChecked property to get or set the state of the switch.
- To respond to changes in the switch state, you can use the OnSwitch event.
Here’s an example of how you might use a TSwitch component in a Delphi program:
1 2 3 4 5 6 7 |
procedure TForm1.Switch1Switch(Sender: TObject); begin if Switch1.IsChecked then ShowMessage(‘The switch is in the On state.’) else ShowMessage(‘The switch is in the Off state.’); end; |
In this example, the OnSwitch event is handled by the Switch1Switch procedure. This procedure checks the state of the switch using the IsChecked property and displays a message box accordingly.
You can also use the OnChangeTracking event to respond when the user starts or stops dragging the switch.
You can also customize the appearance of the switch by setting the StyleLookup property to a custom style. Additionally, you can use the custom styles to make the switch look different in different states, like using different colors for on and off states.
You can also use the Align property to specify where on the container the switch should be displayed, and use the Size property to set the size of the switch.
TSwitch component also has other properties and methods that can be used to customize and interact with the switch control.
- The AutoCapture property controls whether the switch should automatically capture the mouse when the user starts dragging the switch.
- The TouchExpand property controls whether the switch should expand to fill the available space when it is touched.
- The Track property returns the TTrack object that represents the track of the switch, which can be used to customize the appearance of the track.
- The Thumb property returns the TThumb object that represents the thumb of the switch, which can be used to customize the appearance of the thumb.
- The The TSwitch component also has methods like BringToFront, SendToBack, and Realign to control the position of the switch.
It’s also worth noting that, if you need to have a more complex switch in your application, you might consider using third-party libraries. Some of them have more advanced features like changing the switch shape, customizing the colors and animations.
In general, the TSwitch component is a powerful and flexible control that can be used to create a wide range of toggle switch user interfaces in Delphi. By understanding the properties, methods, and events of the TSwitch component, you can create user interfaces that are easy to use and visually appealing.
Leave a Reply