SegmentedControl

From Xojo Documentation

Class (inherits from RectControl)


New in 2010r4

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

Events
Action DragExit KeyUp
Closing DragOver MouseEnter
ConstructContextualMenu DropObject MouseExit
ContextualMenuAction Initialized MouseMove
DragEnter KeyDown MouseWheel


Properties
Active fa-lock-32.png LockLeft Segments
AllowAutoDeactivate LockRight SelectionType
AllowTabStop LockTop TabIndex
Enabled MacControlStyle Top
Handle fa-lock-32.png MouseCursor Transparent
Height MouseX fa-lock-32.png TrueWindow fa-lock-32.png
HelpTag MouseY fa-lock-32.png Visible
Index fa-lock-32.png Name Width
Items PanelIndex Window fa-lock-32.png
Left Parent
LockBottom Scope fa-lock-32.png


Methods
AcceptFileDrop Close SetFocus
AcceptPictureDrop DrawInto SizeToFit
AcceptRawDataDrop Invalidate
AcceptTextDrop Refresh

Notes

The easiest way to configure a SegmentedControl 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 SelectionType is Multiple, you can have more than one selected segment.)

You set the behavior of the control with the SelectionType 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 SegmentedControlItem class. You instantiate an instance of this class for the segment that you want to address.

Resizing a Segmented Control

The individual segments do not automatically resize when the width of the SegmentedControl 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 SegmentedControl:

Dim cnt As Integer = SegmentedControl1.Items.Ubound + 1
Dim w As Integer = SegmentedControl1.Width
Const kPadding = 2
Dim sw As Integer = (w / (cnt)) - kPadding

For i As Integer = cnt - 1 DownTo 0
// Get the reference to the segment
Dim s As SegmentedControlItem = SegmentedControl1.Items(i)

s.Width = sw
Next

Sample Code

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

Dim s As SegmentedControlItem
s = SegmentedControl1.Items(0)
If s.Selected Then
// code
Else
// more code
End If

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

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

See Also

PushButton, SegmentedControlItem classes; UserGuide:Desktop Segmented Control topic