Slider control

Hi,

here is slider control I made. I made it for pi and touchscreen.
You can change value only by mousedown or mousedrag because that’s all i needed.
Feel free to make it better or what ever :slight_smile:
cSlider.zip

Very nice. On Windows 7, I needed to turn on the DoubleBuffer property to mike it flicker free and get rid of the artifacts. After that works great. Very nice work!

Really nice work!
Thank you.

how did get the value of 78 from the slider into a textbox?

and would i be able to set slider to 78 from code somewhere else?

cSlider1_mouseup event

ScreenPercent.TEXT = cstr(me.svalue)

frmScreenSetup_event

cSlider1.svalue=Val(ScreenPercent.Text)
cSlider1.refresh

edited :
the slider did not jump the proper place… always at the beginning

Like i say you can only set value by mouse events. If you want to change value outside, you have to calculate pix from value. Now i only calculate value from pix.

Jukka

it look amazing… pity i have to remove the slider from my application now since i can’t set value and show properly.

The maximum value could be 300, and you may want to set a value of 212

To set something like 212 explicitly,
Try this for a simple ‘SetValueAbsolute’

[code]Sub SetValueAbsolute(xValue As Integer)

//TODO… add proper bounds checking to ensure no value of > max or less than min is set
Select Case xValue
Case 0
Pos = 0
Case max
Pos = Width - sheight
Case Else
pos = xvalue/ maxvalue * width + Floor(sheight * 0.5 * 0.1)
End Select

me.Invalidate(False)

End Sub[/code]

Jeff, agreed on the proper bounds checking, just posting something quick to help Richard, wasn’t really trying to write production code. But your pos calculation doesn’t set it properly most of the time for me, off a little when I tried. Here is what I originally posted then deleted:

pos = (Width - sheight) * xValue * 0.01 + Floor(sheight * 0.5 * 0.1)

although when the slider is very small, it is off a little bit as well. Should give Richard something to go on.

Yep. The maths is probably a bit off…
I took yours and tweaked it off the top of my head.
I’ll probably spend some love on it over the next day or two… it looks nice enough to persevere with.

this work merv!!
now i am going to into how it can cater for negative in the minvalue

No, that’s is what I originally posted, but it fails on some values when the slider size is really small, say 100 or smaller. I’m still messing with it…

i was trying to do a slider between -25 to +100… it work pretty good when i use the slider width around 200 for 0 to 100

Try this, seems to work OK when the width is at least 100+the button’s width and scaled 0 to 100:

Select Case xValue Case Is <= minValue pos = 0 Case Is >= maxValue pos = Width - sheight Case Else ' pos = user_scale * working_width + button_width * 1/2 * 1/100_scale pos = ((xvalue / maxvalue) * (Width - sheight)) + ((Width - sheight) * 0.5 * 0.01 ) End Select

Hi merv, you code work great if i don’t need the negative value.
i have try all night… giving up and use the original slider.

One small issue with sliders like this is where it is to replace a scroll bar that can represent values outside of the 0…100 range.

For example, negatives, or large values.
This is because the position of the handle is a percentage, and could mean there are values impossible to obtain by dragging the slider around.
So I added little arrows at the end to allow ‘nudge by 1’
This allows you get any value between min and max, even if the slide wont take you there precisely using your thumb.

If this is of use to anyone, here is ‘free as in beer’ code for a different silder. It’s not worked up into a ‘proper’ control as I only wrote it for me. Some properties haven’t been exposed to the IDE for example. Value can be set in code.

If you can use it as a base for something, or as a doorstop, feel free.

Download

Jeff, how do i change the purple color on the left hand side?

Currently hard coded inside the paint event.
You could add properties for the colors

Colors are ‘Some properties haven’t been exposed to the IDE’