Setting the tick length on a Slider control in Flex

In a previous example, “Setting the tick thickness on a Slider control in Flex”, we saw how to change the thickness of a tick on a Flex HSlider control by setting the tickThickness style.

The following example shows how you can set the length of a tick on a Flex HSlider control by setting the tickLength style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/04/setting-the-tick-length-on-a-slider-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:HSlider id="slider"
            minimum="0"
            maximum="10"
            value="3"
            liveDragging="true"
            tickLength="{slider.value}"
            tickInterval="1" />

</mx:Application>

View source is enabled in the following example.

You can also set the tickLength style in an external .CSS file or <mx:Style /> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/04/setting-the-tick-length-on-a-slider-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        HSlider {
            tickLength: 10;
        }
    </mx:Style>

    <mx:HSlider id="slider"
            minimum="0"
            maximum="10"
            value="3"
            liveDragging="true"
            tickInterval="1" />

</mx:Application>

Or, you can also set the tickLength style using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/04/setting-the-tick-length-on-a-slider-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.SliderEvent;

            private function slider_change(evt:SliderEvent):void {
                slider.setStyle("tickLength", evt.value);
            }
        ]]>
    </mx:Script>

    <mx:HSlider id="slider"
            minimum="0"
            maximum="10"
            value="3"
            liveDragging="true"
            tickInterval="1"
            change="slider_change(event);" />

</mx:Application>

0 thoughts on “Setting the tick length on a Slider control in Flex

  1. Hi, I was wondering if there is a component with variable length of tick marks. No they are all with the same length, and I’m looking for something like a chart / scale? Is it possible to make such a component?

    You do great job. Good luck.