How can I set "maxChars" to TextInput in an editable ComboBox?
I want to set a property maxChars
for the TextInput of an editable ComboBox. I am currently truncating the text to a given number of characters using the Change Event:
private function nameOptionSelector_changeHandler(event:ListEvent):void
{
nameOptionSelector.text = nameOptionSelector.text.substr(0, MAX_LENGTH);
}
It looks like overkill. There must be a better way to do it ....
0
a source to share
3 answers
My alternative is to use the protected textInput directly. This approach allows you to set the "maxChars" property in the builder or GUI code just as you would for a regular TextField. Note that zero is a valid value for maxChars and indicates unlimited characters. To avoid trying to set maxChars before the TextInput object exists, you must override .childrenCreated ().
package my.controls
{
import mx.controls.ComboBox;
public class EditableComboBox extends ComboBox
{
public function EditableComboBox()
{
super();
}
private var _maxChars:int = 0;
override protected function childrenCreated():void
{
super.childrenCreated();
// Now set the maxChars property on the textInput field.
textInput.maxChars = _maxChars;
}
public function set maxChars(value:int):void
{
_maxChars = value;
if (textInput != null && value >= 0)
textInput.maxChars = value;
}
public function get maxChars():int
{
return textInput.maxChars;
}
}
}
+2
a source to share
Using Stigler's suggestion, here is the complete solution I followed:
package
{
import mx.controls.ComboBox;
public class ComboBoxWithMaxChars extends ComboBox
{
public function ComboBoxWithMaxChars()
{
super();
}
private var _maxCharsForTextInput:int;
public function set maxCharsForTextInput(value:int):void
{
_maxCharsForTextInput = value;
if (super.textInput != null && _maxCharsForTextInput > 0)
super.textInput.maxChars = _maxCharsForTextInput;
}
public function get maxCharsForTextInput():int
{
return _maxCharsForTextInput;
}
override public function itemToLabel(item:Object):String
{
var label:String = super.itemToLabel(item);
if (_maxCharsForTextInput > 0)
label = label.substr(0, _maxCharsForTextInput);
return label;
}
}
}
0
a source to share