# Path to Segmented Control?

**URL:** <https://forum.xojo.com/t/path-to-segmented-control/17686>\
**Category:** General\
**Created:** [July 3, 2014, 11:10am UTC](https://forum.xojo.com/t/path-to-segmented-control/17686 "2014-07-03T11:10:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Summers](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_summers/32/477_2.png) [@Richard\_Summers](https://forum.xojo.com/u/Richard_Summers)\
**Post date:** [July 3, 2014, 11:10am UTC](https://forum.xojo.com/t/path-to-segmented-control/17686/1 "2014-07-03T11:10:04Z")

</div>

I have a SegmentedControl1 and a TextField1 on my window, but when I try to determine which segment is currently selected from with the TextField’s TextChange event, I get a “this item does not exist” error?

In the TextField code I have:

`If SegmentedControl1.itemIndex = 0 then
MsgBox("segment 0 ")
else
MsgBox("segment 1 ")
End if`

My code above works perfectly in the SegmentedControl’s action event, but not from the TextField?  
Thank You.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 3, 2014, 11:51am UTC](https://forum.xojo.com/t/path-to-segmented-control/17686/2 "2014-07-03T11:51:48Z")

</div>

[quote=107417:@Richard Summers]I have a SegmentedControl1 and a TextField1 on my window, but when I try to determine which segment is currently selected from with the TextField’s TextChange event, I get a “this item does not exist” error?

In the TextField code I have:

`If SegmentedControl1.itemIndex = 0 then
MsgBox("segment 0 ")
else
MsgBox("segment 1 ")
End if`

My code above works perfectly in the SegmentedControl’s action event, but not from the TextField?  
Thank You.[/quote]

You see, Richard, your problem is related to the EventID problem you had yesterday. Some variables are only available within the event. In the case of the SegmentedControl, look at the top of the action event : Action(itemindex as integer).

The scope of ItemIndex is limited to the event. It is not a property of SegmentedControl.

If you want to know what was the last value of ItemIndex, create a global property in a module, for instance, LastItemIndex, and set it in the action event as such

```auto
LastItemIndex = ItemIndex
```

Then you can use LastItemIndex anywhere in your program. You can also create LastItemIndex in the window, its scope will then be limited to the window.

---

<div class="post-metadata">

**Author:** ![Richard\_Summers](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_summers/32/477_2.png) [@Richard\_Summers](https://forum.xojo.com/u/Richard_Summers)\
**Post date:** [July 3, 2014, 11:54am UTC](https://forum.xojo.com/t/path-to-segmented-control/17686/3 "2014-07-03T11:54:15Z")

</div>

Ahhhhhh - That makes things a lot clearer.  
I learn something every day - Thank You!

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [July 3, 2014, 6:48pm UTC](https://forum.xojo.com/t/path-to-segmented-control/17686/4 "2014-07-03T18:48:57Z")

</div>

If you selection type is Single, you can loop through the Items() of the segmented control and find the one that is selected.

```auto
Function Value(Extends s as segmentedcontrol) as integer
   for i as integer = 0 to s.items.ubound
      if s.items(i).selected then return i
   next
   return -1 // no selection
end function
```

---

<div class="post-metadata">

**Author:** ![Richard\_Summers](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_summers/32/477_2.png) [@Richard\_Summers](https://forum.xojo.com/u/Richard_Summers)\
**Post date:** [July 3, 2014, 6:56pm UTC](https://forum.xojo.com/t/path-to-segmented-control/17686/5 "2014-07-03T18:56:05Z")

</div>

Thanks Tim.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:18pm UTC](https://forum.xojo.com/t/path-to-segmented-control/17686/6 "2020-10-29T17:18:24Z")

</div>


