March 16th, 2007 by Kyle
Tags: , , , ,
Posted in: Flex

I wrote a simple little extension to ComboBox to allow the dropdown to expand to the width of the widest entry and the ComboBox input to expand/contract to the width of the selected item. This is based on the simple ComboBox sample from the docs found here.

Here is the source code for the component:

<?xml version="1.0" encoding="utf-8"?>
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" change="onChange(event);">
    <mx:Script>
        <![CDATA[
       
            import mx.controls.ComboBase;
            import mx.core.EdgeMetrics;
       
            private function init():void{
                this.dropdownWidth=calculatePreferredSizeFromData(this.dataProvider.length).width;
            }
           
            private function onChange(e:Event):void{
                var txt:String = itemToLabel(selectedItem);
                if(measureText(txt).width > 0){
               
                    var buttonWidth:Number = getStyle("arrowButtonWidth");

                    // Text fields have 4 pixels of white space added to each side
                    // by the player, so fudge this amount.
                    // If we don’t have any data, measure a single space char for defaults

                    var bm:EdgeMetrics = borderMetrics;

                    var textWidth:Number = measureText(txt).width + bm.left + bm.right + 8;
                    this.width = textWidth + buttonWidth;
                }    
                else {
                    this.width=DEFAULT_MEASURED_MIN_WIDTH
                }
            }
        ]]>
    </mx:Script>
</mx:ComboBox>
 

Read the rest of this post»


No Comments »

I had a customer request some help in modifying the default Flex ComboBox behavior such that when the dropdown list was open and a user was scrolling the mousewheel with the mouse outside the dropdown list, the dropdown list would scroll. The default behavior is to close the dropdown list as soon as the user scrolls the mousewheel outside the dropdown list.

Read the rest of this post»


No Comments »