Category / Section
How to customize the range intervals in WinForms Rangeslider
2 mins read
In RangeSlider control, we would able to set the tick frequency with integer values to show the range in required intervals. But it allows only the integer values. So, if instance to provide the float values or half of the range to be displayed, then TickFrequency can be customized using the following steps.
Step 1: Invoke the DrawLabel event of RangeSlider control.
Step 2: Assign the timespan value.
Step 3: Iterate the timespan value to half of the time according to the e.value provided.
Step 4: Assign to the e.Text property which shows the range label in UI.
Code:
public Form1() { InitializeComponent(); //Invoke the DrawLabel event this.rangeSlider1.DrawLabel += RangeSlider1_DrawLabel; this.rangeSlider1.ShowLabels = true; this.rangeSlider1.Minimum = 1; this.rangeSlider1.Maximum = 10; this.rangeSlider1.TickFrequency = 1; this.StartPosition = FormStartPosition.CenterScreen; } //Customizing the range intervals in RangeSlider private void RangeSlider1_DrawLabel(object sender, DrawLabelEventArgs e) { TimeSpan time2 = new TimeSpan(0,30,00); for (int i = 0; i < e.Value; i++) { time2 = time2.Add(new TimeSpan(0, 30, 0)); } DateTime time = DateTime.Today.Add(time2); //Assign to the text property e.Text = time.ToString("hh:mm tt"); e.Handled = true; }
Output: