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.
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.