Segmented Control With No Segments Selected

Hello.

I typically make database applications. To help ensure accurate data entry by users I prefer to have all controls start in a “no data” state. So a text field would be, obviously, empty, and a segmented control would have no segments selected.

A new project required a bunch of Yes/No entries. I thought a segmented control would be perfect for that, but I couldn’t get it have no segment selected (it would default to the right-most segment being on). After searched around a bit and did not discover any solution. But I did come up with what follows:

How to make a segmented control that starts with no segment selected

  1. Subclass the SegmentedControl

  2. In the open event set up the desired segments, titles, widths. You can add new segments on the fly if you like.

  3. Set the SelectionType to 2 (there does not appear to be named constants for SelectionType). Type 2 means “none”, a segment is highlighted only while the mouse is down.

  4. In the action event (fires when our user clicks a segment), set the SelectionType to 0 (“single”). Then, grab the selected item (you are passed the item index of the segment the user clicked) and set item.selected = true. I also created a property in my subclass to hold the newly selected value. Finally, create an event definition in that you will invoke in the action event (passing the itemIndex) so if you need to do something else when the user clicks a segment, you have a way of catching the event.

What we have now is a SegmentedControl that starts with no segment selected. When the user clicks a segment, that segments becomes highlighted and the control works like a normal segmented control.

Hope this helps someone.

-Bob Gordon

Why do you need to subclass the control?
and what I’m seeing it that NO segments are selected BY DEFAULT

Every time it showed up the rightmost segment was selected. The behavior you describe is what I would expect.

Hi Bob, are you only Windows or Mac?? I just try on mine on Mac and it does not pre select the segment at all.

no selection here ether, which is default, you maybe selected on in the list by mistake?

Glad the control actually works as I would want it to. Thanks.