PDA

View Full Version : Combobox Auto Width Changer Script


goatdubbs
07-23-2006, 07:23 AM
Hi everyone. I spent a little while trying to find out how to stretch a combobox list to the appropriate width dependent on the largest sized combobox item. I have created a function to do this. It takes three arguments:
1) [required] The combobox instance.
2) [optional] Boolean value on whether to stretch the top of the combobox too. Defaults to false.
3) [optional] Integer value for the max length it can stretch to (in case one of the combobox items is larger than the Stage). Defaults to stretching to the end of the stage.


function autoWidthComboBox()
{
var theComboBox = arguments[0];
if(theComboBox)
{
var stretchTop = arguments[1] ? arguments[1] : false;
var maxWidth = arguments[2] ? arguments[2] : Math.abs(Stage.width - theComboBox._x) - theComboBox.downArrow_mc._width;
var currBiggestWidth = 0;
for(i = 0; i < theComboBox.dropdown.length; i++)
{
theComboBox.selectedIndex = i;
var currItemWidth = theComboBox.textField.focusTextField.textWidth;
if(currItemWidth + theComboBox.downArrow_mc._width < maxWidth)
{
if(currItemWidth > currBiggestWidth)
{
currBiggestWidth = currItemWidth;
trace(currBiggestWidth);
}
}
else
{
currBiggestWidth = maxWidth;
break;
}
}
theComboBox.selectedIndex = 0;
if(stretchTop)
{
theComboBox._width = currBiggestWidth + theComboBox.downArrow_mc._width;
}
theComboBox.dropdownWidth = currBiggestWidth + theComboBox.downArrow_mc._width;
}
}



Hope this helps someone! It definately helped me. :)
Let me know if you have any questions about it.

MichaelxxOA
07-23-2006, 07:34 AM
Welcome to the Forums!

Good job, I haven't used it yet, but I'm sure it works well...

Take care.
Michael

farafiro
07-23-2006, 07:45 AM
goatdubbs
welcome to the forums

thanks for stopping by and sharing

pisco
03-27-2007, 03:33 PM
thank you very much :) very usefull snippet, dunno why flash devs didnt include it :eek:

papercrate
06-04-2009, 01:08 PM
Thanks for this, I'd really like to use it but when I publish or export my movie I get this error:

1119: Access of possibly undefined property width through a reference with static type Class.

It is in relation to this line in the AS:
var maxWidth = arguments[2] ? arguments[2] : Math.abs(Stage.width - nav_cb._x) - nav_cb.downArrow_mc._width;

papercrate
06-04-2009, 01:11 PM
I just realized how old this original post was, does the ComboBox component automatically resize based on content nowadays? It doesn't seem to for my items (with embedded font) populated by a php feed.