SegmentedButton

From Xojo Documentation

Class (inherits from RectControl)


New in 2019r2

A control that is a horizontal button made up of multiple, independent segments.

Events
Close DragOver MouseExit
ConstructContextualMenu DropObject MouseMove
ContextualMenuAction KeyDown MouseWheel
DragEnter KeyUp Open
DragExit MouseEnter Pressed
Properties
Active fa-lock-32.png LockTop SelectedSegmentIndex
AllowAutoDeactivate MacButtonStyle SelectionStyle
AllowTabStop MouseCursor TabIndex
Enabled MouseX fa-lock-32.png Tooltip
Handle fa-lock-32.png MouseY fa-lock-32.png Top
Height Name fa-lock-32.png Transparent
Index fa-lock-32.png PanelIndex TrueWindow fa-lock-32.png
LastSegmentIndex fa-lock-32.png Parent Visible
Left Scope fa-lock-32.png Width
LockBottom SegmentCount fa-lock-32.png Window fa-lock-32.png
LockLeft Segments
LockRight SelectedSegment fa-lock-32.png


Methods
AcceptFileDrop AddSegmentAt RemoveAllSegments
AcceptPictureDrop Close RemoveSegmentAt
AcceptRawDataDrop DrawInto ResizeSegmentsToFit
AcceptTextDrop Invalidate SegmentAt
AddSegment Refresh SetFocus

Notes

The easiest way to configure a SegmentedButton is to use the Segment Edit dialog in the IDE by clicking "Edit" in the Inspector. With it, you can create, delete, or reorder segments, label them, add an icon, and choose the selected segment or segments. (If the SelectionStyle is Multiple, you can have more than one selected segment.)

You set the behavior of the control with the SelectionStyle property. It enables you to choose whether the set of segments behaves as a group of radio buttons, a group of checkboxes, or a group of pushbutton. If it behaves like a group of checkboxes (Multiple), more than one can be selected at the same time. If it behaves like a pushbutton (None), then it is "selected" only while the mouse is held down.

If you want to do this work using code, you can address each segment with the Segment class. You instantiate an instance of this class for the segment that you want to address.

Resizing a Segmented Button

The individual segments do not automatically resize when the width of the SegmentedButton changes. You'll need to individually change the widths of each segment to to fit the new width of the control. This code (in the Resized event handler of a Window) resizes the segments to fit the new width of the SegmentedButton:

Var cnt As Integer = SegmentedButton1.LastSegmentIndex
Var w As Integer = SegmentedButton1.Width
Const kPadding = 2
Var sw As Integer = (w / (cnt)) - kPadding

For i As Integer = cnt - 1 DownTo 0
// Get the reference to the segment
Var s As Segment = SegmentedButton1.SegmentAt(i)

s.Width = sw
Next

Sample Code

If you have placed an instance on a window, called SegmentedButton1, you can set or get the properties of any one segment with code like the following

Var s As Segment
s = SegmentedButton1.SegmentAt(0)
If s.Selected Then
// code
Else
// more code
End If

When a segment is selected the Pressed event runs and is passed the index of the segment that was pressed:

If segmentIndex = 0 Then
// code for when segment 0 is selected
Else
// code for when segment 1 is selected
End If

See Also

PushButton, Segment classes; UserGuide:Desktop Segmented Control topic