Android
iOS
 

Specify Slider Value

This recipe shows how to specify the range of a slider and access its value.

Recipe

To create a slider and set its initial value:

1. Create a UISlider and add it to the View:

slider = new UISlider(new RectangleF(100,  30, 210, 20));
View.Add (slider);

2. Set the slider minimum, maximum and initial value:

slider.MinValue = 0f;
slider.MaxValue = 1f;
slider.Value = 0.5f; // initial value

3. Attach an event to respond to any changes in the slider:

slider.ValueChanged += HandleValueChanged; // defined below

4. Create a method to handle the changed value. In the example the value is displayed in a label as the slider is dragged:

void HandleValueChanged (object sender, EventArgs e)
{   // display the value in a label
   label.Text = slider.Value.ToString ();
};

The sample looks like this when running: