Setting a selection disabled color on a ComboBox control’s dropdown menu in Flex

by Peter deHaan on July 15, 2008

The following example shows how you can set a background color for a selected item in a disabled Flex ComboBox control by setting the selectionDisabledColor style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/15/setting-a-selection-disabled-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.enabled = checkBox.selected;
                comboBox.dropdown.setStyle("selectionDisabledColor", 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="enabled:">
                <mx:CheckBox id="checkBox"
                        selected="true" />
            </mx:FormItem>
            <mx:FormItem label="selectionDisabledColor:">
                <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.

You can also set the selectionDisabledColor 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/07/15/setting-a-selection-disabled-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.enabled = checkBox.selected;
            }
        ]]>
    </mx:Script>

    <mx:Style>
        ComboBox {
            dropdownStyleName: myComboBoxDropdownStyleName;
        }

        .myComboBoxDropdownStyleName {
            selectionDisabledColor: red;
            fontWeight: normal;
        }
    </mx:Style>

    <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="enabled:">
                <mx:CheckBox id="checkBox"
                        selected="true" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

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

</mx:Application>

{ 10 comments… read them below or add one }

Klaus Busse July 16, 2008 at 3:11 am

Cool… is there a way to prevent the short white flash when opening the dropdown?

Klaus

Reply

peterd July 16, 2008 at 7:14 am

Klaus Busse,

One workaround is to set the ComboBox control’s openDuration style to 0 so that the ComboBox opens immediately instead of animating down.

Peter

Reply

Mielno July 16, 2008 at 1:44 pm

Thanks for all. I’ll try it and write something more later. Bye – Mielno

Reply

Shahaji S.S. July 16, 2008 at 11:38 pm

Dear All,
If you see carefully, there is one bug in this,
Put your finger on downarrow, click on combobox and immidiately press downarrow, you are able to change the combo value after checkbox state is disabled.

what will be the solution????
shahazee

Reply

peterd July 17, 2008 at 12:17 am

Shahaji S.S.,

Yeah, I noticed this shortly after I posted the example. The solution is to disable keyboard navigation for the Flex ComboBox control. And as luck would have it, that was the subject of the very next blog post, “Disabling keyboard navigation on the ComboBox control in Flex”.

Good catch!

Peter

Reply

meir August 26, 2008 at 1:09 pm

Do you guys know how will it be possible to have a dropdown menu with checkbox next to each option in the dropdown?

Reply

peterd August 26, 2008 at 1:44 pm

meir,

In order to do that, you’ll need to define an item renderer. For more information, and lots of examples, check out the item renderers section of Alex Harui’s blog at http://blogs.adobe.com/aharui/item_renderers/

Peter

Reply

meir August 27, 2008 at 12:31 am

Thanks so much,

Meir

Reply

Jovica Aleksic September 13, 2009 at 7:02 am

@Klaus Busse’s question:
Even a openDuration of 0 will cause the short bright flicker.The key to this is using a custom downSkin.
In my case it was fine to extend UIComponent for that, maybe it would be more correct to use ProgrammaticSkin, however this solution worked fine for me:

package  
{ 
	import mx.core.UIComponent;
 
 
	public class DownSkin extends UIComponent
	{
		public function DownSkin()
		{
			super();
		}
 
		private var downBgColor:Number = 0x222222;
 
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void 
		{
		   super.updateDisplayList(unscaledWidth, unscaledHeight);			
 
			var backgroundFillColor:Number;	
			var cornerRadius:Number = getStyle("cornerRadius");
			var backgroundAlpha:Number = getStyle("backgroundAlpha");
 
			graphics.clear();
 
			switch (name) { 
				case "downSkin":
					backgroundFillColor = this.downBgColor;
					break; 
			}
 
			graphics.beginFill(backgroundFillColor);
			drawRoundRect(0,0,unscaledWidth,unscaledHeight,{tl: cornerRadius, tr: cornerRadius, bl: cornerRadius, br: cornerRadius},
			 				backgroundFillColor,backgroundAlpha);
			graphics.endFill();
 
		 }
	}
}

Reply

Jovica Aleksic September 13, 2009 at 7:05 am

Ah, and of course you need to specify the downSkin for your combobox:

<mx:ComboBox
	downSkin="{DownSkin}" dataProvider="{myDataProvider}">
</mx:ComboBox>

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: