Programming a Slider

I’m programming a slider using the Slider.ValueChanged event. Works great.
But in my case when the Slider.Value reaches a certain threshold value I need to exit the ValueChanged event and go to a sub. How do I do this? Thanks in advance for anybody who can help.

if me.value>xyz then
exit sub
end if

But since ValueChanged is an Event you have to control how that event is dealt with

Thanks Dave,

But my problem is how to exit an Event, not a sub.

I’m not sure what you mean by “control how that event is dealt with”.

Could you give me some help on how to control events, as contrasted to subs?

Thanks again for your help.

You “deal with the event” by writing your code in the event handler, which in your case is Slider.ValueChanged. The code might look like this:

If Me.Value > myThresholdValue Then MethodToCall // A method defined on the window End If

An event “exits” the same as a subroutine exits: when the end of the code is reached or there is a return.

Or ‘exit’ is used.

Well, who does that? :slight_smile:


The problem with the code you suggest is that if the user continues to hold on to the slider cursor after the threshold value has been reached you get an endless series of calls to the Method in the If…Then statement until the slider cursor is released. What is needed is something that shuts down the Slider.ValueChanged event when the threshold has been reached, regardless of whether the user still has the mouse down on the slider cursor.

I’ve tried the Control.Close event but that appears to shut down the Slider control completely. But it needs it to be available to the user once the action in the Method has taken place, and the user clicks on the cursor again.

Using the Exit command doesn’t seem to work either.

That sounds like a weird use case for a Slider.

If you want to stop processing the event, then you’ll need to set a value to indicate that so you can not process your code. Perhaps have a property on the Window that says “StopSlider As Boolean”. Then you can have code like this:

If Not StopSlider Then If Me.Value > myThresholdValue Then StopSlider = True MethodToCall End If End If

To “reactivate” the Slider, you’d set StopSlider = False somewhere.

But why not just set MAXIMUM of the slider to your “threshold”… seeing as it seems you don’t want the user to pass that value anyways?

I do :slight_smile:

Paul: Sorry this sounds weird, but it’s a key feature of the VB6 program I’m converting. We were able to do it in VB6, but I’ve forgotten how, and the source code is lost.

Dave: For reasons I can’t go into getting the desired result by using the Maximum value isn’t relevant in this case. But thanks for the suggestion.

I’m off for the weekend, but will try the approach Paul suggests on Monday. It sounds promising.

Thanks again for all your good help.


I’m working with your suggestion but I still have the problem that the program continues to call the Method recursively until the slider cursor is released from the mouse. (BTW I get the same result by using the Slider.Enabled boolean).

How do I tell the program to release the mouse from the slider cursor as soon as the threshold value is released?

I get the same result whether I use the Slider.Maximum property, or specify a threshold value less than the Maximum value.

Your help is much appreciated.

You cannot tell your app to “release” the mouse. The user controls the mouse.

I’m afraid I just don’t understand what it is you are actually trying to do. Maybe you could put together a quick sample of what you’ve done for us to look at?


For now I have a solution that involves a manual click, which preferably would be unnecessary, but for now we can live with. Once the program conversion is further along we will probably revisit this issue, but for now we’re moving ahead.

Thanks again.