Modifying SeqmentedControl via Code

HI

I want to use a seqmentedcontrol to dynamically handle a number of segments being added at run time.

Also, the control should size to the width of its parent and evenly size the segments across the width of the control.

So far, I can add new segments, etc. but what I can’t do is get the control to size to the parent’s width and lock in place so it resizes with the window.

I must be doing something wrong, but can someone point me in the right direction?

The code/direction I am looking for is to just do the resize and lock edges.

Many thanks

Phil

Simply resize all the segments after you’ve added one:

[code]
Dim seg As New SegmentedControlItem
seg.Title = "Seg " + Str(SegmentedControl1.Items.UBound + 1)

SegmentedControl1.Items.Append(seg)

// Resize all the segments
For i As Integer = 0 To SegmentedControl1.Items.UBound
Dim item As SegmentedControlItem = SegmentedControl1.Items(i)
item.Width = SegmentedControl1.Width / (SegmentedControl1.Items.UBound + 1) - 2
Next[/code]

Thanks Paul; that resizes them perfectly But…

Should the SeqmentedControl respect the lock right/left settings? In the Designer, if I set it to lock to the right, resizing the window in the design resizes the SeqmentedControl. However, when I run the app the control doesn’t resize with the window.

Just created a new desktop app, dropped the control on, sized to window width and locked the edges. Run the app and resize the windows to see what I mean

Is this a bug or something I have missed?

thanks

Phil

Looks like a bug: feedback://showreport?report_id=20563

Drat! Given this bug is about 3 years old I don’t suppose it will be fixed anytime soon :frowning:

Thanks for the heads-up Paul; think I need to change my thinking

Phil

Looks like you can increase the size of the SegmentedControl if you add more segments that would result in the overall width increasing. Perhaps with some math you can figure out what size to use for each individual segment (based on the size of the window) that would in effect allow the segmented control to increase or decrease in size.

I assume I would need to handle the Windows resize to resize it if the parent window size changes. Would it be best I wonder to handle it in the Resized or Resizing event ?

Resizing seems to work for me. Here’s a quick example project:

https://www.dropbox.com/s/hbj0btytdy96uq2/SegmentResize.xojo_binary_project?dl=0

Thanks Paul, made a few tweaks to handle controls centered in the window and it 80% works …

For i As Integer = 0 To ShowFilter.Items.UBound
    Dim item As SegmentedControlItem = ShowFilter.Items(i)
    item.Width = (Self.Width - (ShowFilter.left * 2))  / (ShowFilter.Items.UBound + 1) - 2
  Next
  ShowFilter.SizeToFit

The 20% not working is if I then go full screen with the app and then it seems the Resizing event isn’t called ?

Ah fixed the 20% by calling the resize from Resized event as well. Not sure if calling from both is overkill but seems to work

In RubberViews, I do pretty much what you came up with. Resizing is very much resource demanding, and also may fire too often for your code to execute. I actually do it every two ticks, which is 1/30th a second. Anything faster would be wasted to the human eye.

It is good to do it in Resized event too, where values may differ from the last Resizing.

Finally, I would refrain from doing it in Resizing if it is under Windows, to avoid flicker. On Mac it is fine.