Can a slider return a double?

Hello all,

I’m building a Desktop app for OSX with Xojo 2016 release 2.1. I would like to use a slider to control the system volume. I’m finding that in the ValueChange Event of the slider, I can only return an integer or whole number (1, 2, 3, 4 …). Can a slider return a double or a number that has a decimal point with values between the whole numbers? Example I’d like to return the value of 7.25, however I’m only able to return 7 or 8. Second question would then be can I set the position of the slider with a double rather than an integer?

Thank you in advance for sharing some knowledge.
Will

no you can’t
but you can adapt the returned integer to a double
let’s say you want your slider to move from zero to 100
if you divide the returned integer by 10, then you get a value from 0.0 to 10.0 and you can have the 7.2 value.
choose the min and max values at zero and 400 and you can get a 7.25 value returned if you divide the integer by 40.

Yes, you could. NSSlider uses doubles in Objective-C. But the implementation would be difficult, because you’d interfere with the Xojo framework. Jean-Yves’ proposition is much better.

I use Jean-Yves’ little trick, too.
Instead of the slider I used the scrollbar. But it comes down to the same point.

My scrollbar needs to scroll through a timeline, based on seconds. What I did, was converting the scroll values to milliseconds (multiplying by 1000). So a 10 second timeline would have 10’000 milliseconds, I subtract the viewport width et voila, that is my maximum value. The minimum remains 0. The value of the scrollbar represents the time offset value in milliseconds. So, when changing that value, I must divide by 1000 to have the double value representing the seconds.

But I read somewhere that double values are not so accurate. Would it be wiser to use the currency datatype instead?
My project got too huge to change the whole system to use milliseconds.

you could also subclass slider and store your own properties as a double if you really need it.

Is it an idea to subclass the slider?

In your own version of the slider, you could add computed properties that do the calculations, like changing the values by, lets say, 1000. And when getting the values divide them?

Private Const kDoublePrecision = 1000

Property DoubleMaximum As Double
Get
  Return self.Maximum / kDoublePrecision
End Get
Set
  self.Maximum = value * kDoublePrecision
End Set


Property DoubleMinimum As Double
Get
  Return self.Minimum / kDoublePrecision
End Get
Set
  self.Minimum = value * kDoublePrecision
End Set


Property DoubleValue As Double
Get
  Return self.value / kDoublePrecision
End Get
Set
  self.Value = value * kDoublePrecision
End Set

Ha, Mr Pochez beat me to it…

Example

Awesome!! these are all great suggestions and thank you for the detailed examples and explanations. I’ll look into adding the example that Edwin van den Akker sent along. Thats a great example that does exactly what I thought the slider would do naturally. I do like the approach that Jean-Yves Pochez suggest of some basic math to get the value that I am looking for.

Thanks again everyone!

Anytime.
I actually am using a version of this subclass I made.
I turned the constant “kPrecision” into a property. It provides in a higher or lower decimal resolution, whenever needed.
I subclasses the scroll bar the same way.

I guess I have to thank YOU for the inspiration :wink:

Here is a free more generalized slider class that also handles textual entry/display of the value, and adds some other features.

http://www.katkosoft.com/Ultimate%20Slider/Ultimate%20Slider%202.html

I wrote the first version back around 2001 and updated it in 2009. It still works fine (but you need to add a bundle ID for OSX for the example - it was pre Cocoa).

  • Karen