05
Jul
08

Setting the icon color on a ComboBox control’s dropdown menu in Flex

In a previous example, “Setting the icon color on a ComboBox control in Flex”, we saw how you could set the icon (arrow) color on a Flex ComboBox control by setting the iconColor style.

The following example shows how you can set the icon color of the arrow icons in the Flex ComboBox control’s dropdown menu only without changing the icon color of the ComboBox arrow itself by setting the iconColor style on the ComboBox control’s dropdown property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/05/setting-the-icon-color-on-a-combobox-controls-dropdown-menu-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

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

            private function comboBox_open(evt:DropdownEvent):void {
                comboBox.dropdown.setStyle("iconColor", colorPicker.selectedColor);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
        <mx:Object label="Seven" />
        <mx:Object label="Eight" />
        <mx:Object label="Nine" />
        <mx:Object label="Ten" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="iconColor:">
                <mx:ColorPicker id="colorPicker" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ComboBox id="comboBox"
            dataProvider="{arr}"
            open="comboBox_open(event);" />

</mx:Application>

View source is enabled in the following example.

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/05/setting-the-icon-color-on-a-combobox-controls-dropdown-menu-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.events.DropdownEvent;
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ColorPicker;
            import mx.controls.ComboBox;
            import mx.events.ColorPickerEvent;

            private var arr:Array;
            private var colorPicker:ColorPicker;
            private var comboBox:ComboBox;

            private function init():void {
                arr = [];
                arr.push({label:"One"});
                arr.push({label:"Two"});
                arr.push({label:"Three"});
                arr.push({label:"Four"});
                arr.push({label:"Five"});
                arr.push({label:"Six"});
                arr.push({label:"Seven"});
                arr.push({label:"Eight"});
                arr.push({label:"Nine"});
                arr.push({label:"Ten"});

                colorPicker = new ColorPicker();

                var formItem:FormItem = new FormItem();
                formItem.label = "iconColor:";
                formItem.addChild(colorPicker);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                Application.application.addChildAt(appControlBar, 0);

                comboBox = new ComboBox();
                comboBox.dataProvider = arr;
                comboBox.addEventListener(DropdownEvent.OPEN, comboBox_open);
                addChild(comboBox);
            }

            private function comboBox_open(evt:DropdownEvent):void {
                comboBox.dropdown.setStyle("iconColor", colorPicker.selectedColor);
            }
        ]]>
    </mx:Script>

</mx:Application>

2 Responses to “Setting the icon color on a ComboBox control's dropdown menu in Flex”


  1. 1 Amanda Oct 20th, 2008 at 9:44 am

    Just wanted to let you know that this example seems to not be working. Thanks for all of your help, I use these tips almost daily!

  2. 2 Amanda Oct 20th, 2008 at 9:47 am

    Oops, I should read properly before opening my mouth. This works as described. Thanks!

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".