SegmentedControl.SizeToFit

From Xojo Documentation

Method

SegmentedControl.SizeToFit()

New in 2010r4

Supported for all project types and targets.

Sizes the control to fit into its space, increasing or decreasing its width as necessary. Use this after making changes to the SegmentedControlItem width.

Example

If you add a new segment, the SegmentedControl does not resize to fit it so you need to call SizeToFit:

// Add a new segment
Dim seg As New SegmentedControlItem
seg.Title = "Seg " + Str(SegmentedControl1.Items.Ubound + 1)
SegmentedControl1.Items.Append(seg)

SegmentedControl1.SizeToFit

If you need to keep the SegmentedControl width the same then you will want to resize the individual segments to fit within the SegmentedControl width. Refer to the SegmentedControl.Items method for an example on how to do that.

This example demonstrates how to dynamically change a SegmentedControl's properties on the fly. For example, this code on a PushButton increases the size of the selected segment. The code cycles through all the segments and increases the size of each segment that is selected. The loop creates an instance of a SegmentedControlItem for each segment, tests whether it is selected using the Selected property, and then increases the width for only the selected ones. This will increase the overall width of the SegmentedControl:

// count down to avoid re-evaluating the Ubound all the time
For i As Integer = SegmentedControl1.Items.Ubound DownTo 0

// get the reference to the segment
Dim s As SegmentedControlItem = SegmentedControl1.Items(i)

// see if the segment was selected
If s.Selected Then
// it is selected so increase this segment in size
s.Width = s.Width + 10
End If
Next

// make sure the segmented control knows to resizes its drawing boundaries or you can get weird effects
SegmentedControl1.SizeToFit