Programmatically select item in Segmented Control

I use a segmented control (single select - radio button style) and I want to pre-select an item upon opening the control. I thought this would be easy using code like this in the Open event:

[code] Dim s As SegmentedControlItem = me.Items(0)

If XValueAsTime Then
s.Selected=True
Else
s.Selected=False
End If

me.Refresh
[/code]
However; nothing happens. None of the items are selected.

What am I doing wrong here?

What OS ?
It works here on 10.8.4 in a quick copy & paste of your code

Same OS…

Strange… I just tested in a new project with only the segmented control. And yes; it works as expected. But it still won’t work in my real project. It goes through the open event as it should, but none of the segments are selected.

I think it is a timing issue. For example, in your real project put a test button and put your code in the action event.
Fire up the project. Wait for everything to paint and then press the test button, it should set the value.

Argh… As (almost) usual it’s a bug on my behalf. It just dawned on me my logic is wrong. “XValueAsTime” defaults to false, so - logically - s.selected is also set to false.

Also, the other segment should also be set as well.

My bad :slight_smile:

Hi, I am trying the code that was posted in the original message, and the debugger returns an error “This item does not exist.”

The error is referring to XValueAsTime

Any suggestions?

Thank you.

this is what i did on the OnOpen of the Window. I loop the item on the segment control and set the value =0 (first item) and run the codes

  For i AS Integer = SegmentedControl1.Items.Ubound DownTo 0
    DIM s AS SegmentedControlItem = SegmentedControl1.Items( i )
    if i=0 then 
      s.Selected=true
      vType="Status"
      call SetOption
      Exit For
    End If
  Next

[quote=40140:@VALERIY KOZMENKO]Hi, I am trying the code that was posted in the original message, and the debugger returns an error “This item does not exist.”

The error is referring to XValueAsTime

Any suggestions?

Thank you.[/quote]

The snippet is missing the declaration of XValueAsTime so cutting & pasting it isn’t going to work
You’d have to define it and set a value (true or false) then the code will work
Its what I had to do to initially try the code as posted