SegmentedControl: Resize does not fire

Hi everyone,

during testing my StackView Classes under Mint Linux 18.2 (GTK-3) I realized, that if a SegmentedControl has the properties

LockRight = True LockRight = True

the Resize-Event of the control/Window does not fire. Looks like the same problem with the Windows/ContainerControls Resize-Event which also doesn’t fire. Any ideas to get it work?

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 and then call SizeToFit to ensure the segments are sized appropriately. This code (in the Resized event handler of a Window) resizes the segments to fit the new width:

[code]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

// Resize the control to fit its segments
SegmentedControl1.SizeToFit[/code]

Thank you @Paul Lefebvre . That’s pretty nice and it works fine, but everytime I resize the Window/ContainerControl, the SegmentedControl will shrink 2 Pixels on macOS and on Mint Linux it grows up 2 Pixel everytime…

You don’t need the SizeToFit at the end; I copied the wrong code there. I updated the page here with what was working for me:

http://documentation.xojo.com/index.php/SegmentedControl

Still, you may need to adjust the padding for platform or switch to different math that calculate %.

Now I’ll get a better result, but if I change the Window-Size on Linux, the Resize-Event won’t fire :confused: It only fires one time when the App opens.

[quote=347982:@Paul Lefebvre]You don’t need the SizeToFit at the end; I copied the wrong code there. I updated the page here with what was working for me:

http://documentation.xojo.com/index.php/SegmentedControl

Still, you may need to adjust the padding for platform or switch to different math that calculate %.[/quote]
Without calling SizeToFit, The control won’t resizing.

You are right, but by using SizeToFit, the Control grows up everytime 2 Pixels.

That’s because of when you resize SegmentedControlItem and call SizeToFit , SegmentedControl will be resized too.

Edit:
This statement is not all true. SizeToFit is only working when

itemCount * modified SegmentedControlItem.Width + padding <> current SegmentedControl.width

Ok, and what should I modify in the code above?

I assume that you know the relative width of your SegmentedControl to your window.

SegmentedControl1.Left = 20
//set to arbitrary width so that SizeToFit works
SegmentedControl1.Width = 100

dim padding as Integer = 8
dim count as Integer = SegmentedControl1.Items.UBound+1
dim w as Integer = (self.Width - 20*2 - padding)/count

For i As Integer = count-1 DownTo 0 
        Dim s As SegmentedControlItem = SegmentedControl1.Items(i)
        s.Width = w
Next

SegmentedControl1.SizeToFit()

Or you can use this instead

dim scw as Integer = SegmentedControl1.Width

SegmentedControl1.Left = 20
//set to arbitrary width so that SizeToFit works
SegmentedControl1.Width = 100

dim padding as Integer = 8
dim count as Integer = SegmentedControl1.Items.UBound+1
dim w as Integer = (scw - padding)/count

For i As Integer = count-1 DownTo 0 
    Dim s As SegmentedControlItem = SegmentedControl1.Items(i)
    s.Width = w
Next

SegmentedControl1.SizeToFit()

Thanks Paul and Asis,

it works at the moment on macOS and Linux with this code:

[code]Dim cnt As Integer = PropertySegment1.Items.Ubound + 1
Dim w As Integer = PropertySegment1.Width
Const kPadding = 2
Dim sw As Integer

#If TargetLinux
Dim l As Integer = PropertySegment1.Left
sw = (w - l * 2 - kPadding) / cnt
#Else
sw = (w / (cnt)) - kPadding
#EndIf

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

#If TargetCocoa
sw = sw - 2
#ElseIf TargetLinux
sw = sw - 1
#EndIf

s.Width = sw
Next

#If TargetLinux
PropertySegment1.SizeToFit
#Endif[/code]

I hope now, that it will also run on Windows!