SegmentedButton.ResizeSegmentsToFit

From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
Method

SegmentedButton.ResizeSegmentsToFit()

New in 2019r2

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 Segment width.

Example

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

// Add a new segment
Var seg As New Segment
seg.Title = "Seg " + Str(SegmentedButton1.LastSegmentIndex + 1)
SegmentedButton1.AddSegment(seg)

SegmentedButton1.ResizeSegmentsToFit

If you need to keep the SegmentedButton width the same then you will want to resize the individual segments to fit within the SegmentedButton width. This example shows how to do that:

Var seg As New Segment
seg.Title = "Seg " + Str(SegmentedButton1.LastSegmentIndex + 1)

SegmentedButton1.AddSegment(seg)

// Resize all the segments
For i As Integer = 0 To SegmentedButton1.LastSegmentIndex
Var item As Segment = SegmentedButton1.SegmentAt(i)
item.Width = SegmentedButton1.Width / (SegmentedButton1.LastSegmentIndex + 1) - 2
Next


This example demonstrates how to dynamically change a SegmentedButton'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 Segment 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 = SegmentedButton1.LastSegmentIndex DownTo 0

// get the reference to the segment
Var s As Segment = SegmentedButton1.SegmentAt(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
SegmentedButton1.ResizeSegmentsToFit