Thursday, June 10, 2010

Single/Multi line in one Component in Flex 3

In Flex 3 TextInput component allows single line & TextArea allows multi line text input. Want to have an single component which will allow both., then use below code (component)

SingleMutiLineTextArea.as
package components
{
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.TextEvent;
import flash.ui.Keyboard;
import mx.controls.TextArea;

public class SingleMutiLineTextArea extends TextArea
{
public function SingleMutiLineTextArea()
{
super();
this.addEventListener(TextEvent.TEXT_INPUT,onTextInput);
}

private var _multiLine:Boolean = false;

public function set multiLine(value:Boolean):void
{
multiLine = value;
}

public function get multiLine():Boolean
{
return _multiLine;
}

private function onTextInput(event:TextEvent):void
{
if (!_multiLine && (event.text.charCodeAt() == 10 || event.text.indexOf(String.fromCharCode(10))!=-1))
{
event.preventDefault();
}
}
}
}


If the property 'multiLine' is set to 'true' it accepts multiline entering of text., if it is set to 'false' it wont allow.

Leave you comments below if found useful

1 comment: