Specify Slider Value

PDF for offline use
Sample Code:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

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 CGRect(100,  30, 210, 20));
View.Add (slider);
  1. Set the slider minimum, maximum and initial value:
slider.MinValue = 0f;
slider.MaxValue = 1f;
slider.Value = 0.5f; // initial value
  1. Attach an event to respond to any changes in the slider:
slider.ValueChanged += HandleValueChanged; // defined below
  1. 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:

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.