Slider snap to tick marks?

MacOS 10.13.6, Xojo2019r1.1

I have a slider with a small maximum, tick marks on. When I drag the slider, it doesn’t snap to the tick marks. Dragging is smooth and the marker can be placed anywhere, even though there are only 3 or 4 tick values available.

This seems ridiculous to me. It’s been years since I needed a slider that works this way, but I know it used to snap to position. Now it doesn’t. Do I have to force it to snap to values with something like reading mouse events and setting the value property with a timer, or (seems more likely) am I missing something here?

what if you just implement ValueChanged like

Static isChangingAlready As Boolean
If isChangingAlready = True Then
  Return
End If

isChangingAlready = True
Dim tmp As Integer = Me.value
Me.value = tmp
isChangingAlready = False

you could get fancier & have values > ,5 snap up and those <= .5 snap down

[quote=485794:@Norman Palardy]what if you just implement ValueChanged like

Static isChangingAlready As Boolean
If isChangingAlready = True Then
  Return
End If

isChangingAlready = True
Dim tmp As Integer = Me.value
Me.value = tmp
isChangingAlready = False

you could get fancier & have values > ,5 snap up and those <= .5 snap down[/quote]

Hm, thanks, that seems logical, but it doesn’t work. The slider still drags smoothly and can be placed anywhere, doesn’t snap to the ticks.

EDIT: The problem appears to be that the ValueChanged event DOES NOT FIRE unless I drop the pointer on a tick mark. Now that really IS ridiculous. There is a very tiny bit of magnetic-like attraction of the pointer around each tick, but it is so weak as to be unnoticeable.

If Xojo can no longer make a slider snap to values, there must be some declare that can do it. Otherwise there’s no point in using this control for this purpose. I know how to make a custom control that does what I need, but I really shouldn’t have to do that.

what if you turn on live updates ?

dont get me wrong - it should be built in a way that the stickiness to tick marks is built in or at least something we can enable

I tried with LiveScroll on or off, makes no difference.

I’ve successfully subclassed the Slider control to make it snap. Took about 15 minutes.

To set the Maximum, instead of setting the value directly, call SetMaximum( value ). That method needs to be called if the width of the control ever changes. I’ll post a link to download the class.

Enjoy: xSnapSlider

Caveat: It’s horizontal only. If you need a vertical slider, you’ll need to adapt it.

For macOS there is a declare that you can use to get the OS to snap it for you, but I don’t know if it’s possible on Windows or Linux.

Interestingly, it seems to snap to the tick marks fine on Windows 10 with 2019R3.1.

#If targetCocoa
  
  Declare Sub setAllowsTickMarkValuesOnly Lib "Cocoa" selector "setAllowsTickMarkValuesOnly:"  (id As Integer, value As Boolean)
  
  Call setAllowsTickMarkValuesOnly(Me.Handle, True)
  
#EndIf
2 Likes

I wonder if there is a way, on Windows at least, to have the slider NOT stop on the tick marks, or to have the step as a double rather than an integer, so a fractional value can be had. What if I want a value of 2.5 on a 5 step slider?