For the complete documentation index, see llms.txt. This page is also available as Markdown.

Slider

Introduction

The Slider control provides a way for the user to change a value by dragging the slider thumb.

Code Example: Creating a Slider

The following code creates a Slider which allows the user to select a value between 0 and 30, inclusive. The IsSnapToTickEnabled property results in the value being snapped to the TickFrequency value. In this case, the value is used to force whole numbers.

// Initialize
var label = new Label();
label.AddToRoot();
label.X = 50;
label.Y = 24;

var slider = new Slider();
slider.AddToRoot();
slider.X = 50;
slider.Y = 50;
slider.Minimum = 0;
slider.Maximum = 30;
slider.TicksFrequency = 1;
slider.IsSnapToTickEnabled = true;
slider.Width = 250;
slider.ValueChanged += (_, _) => 
    label.Text = $"Value: {slider.Value}";
slider.ValueChangeCompleted += (_, _) => 
    label.Text = $"Finished setting Value: {slider.Value}";

Try on XnaFiddle.NET

Slider reporting its value whenever the value changes or when the change completes

Value

Value is a double which represents the number displayed by the Slider. This value can change in response to UI events, binding, or explicit setting in code. Value is always between Minimum and Maximum, inclusive.

The following code directly sets Value:

Setting a value outside of its bounds forces the value to the bounds. For example, the following code results in a value of 50:

Value can also be changed by changing either Minimum or Maximum:

Clicking the Track

IsMoveToPointEnabled controls what happens when the user clicks the track. It defaults to false.

Stepping with LargeChange

When IsMoveToPointEnabled is false, each click on the track changes Value by LargeChange toward the cursor. Holding the button repeats the step until the thumb reaches the cursor.

Try on XnaFiddle.NET

LargeChange defaults to 25 regardless of Minimum and Maximum. On a slider from 0 to 1, one click jumps the whole range. On a slider from 0 to 1000, one click barely moves the thumb. Set LargeChange to fit your range, or set IsMoveToPointEnabled to true.

Moving to the Cursor

When IsMoveToPointEnabled is true, clicking the track moves the thumb to the cursor. The thumb follows the cursor while the button is held. LargeChange is not used.

Try on XnaFiddle.NET

ValueChanged is raised as the thumb moves. ValueChangeCompleted is raised once when the button is released, so use it for work that should not run every frame, such as saving a setting. IsSnapToTickEnabled applies in both modes.

Last updated

Was this helpful?