How can I disable carriage returns within an editable TextArea?
I am simply wanting to stop a user entering carriage returns in a TextArea. I have been experimenting with the 'restrict' property in the TextArea but cant seem to work it out.
I have the following code:
<mx:Canvas id="cvs1" label="Panel 1" width="100%" height="100%" creationComplete"addEvtListnerOnPlaceText()" backgroundColor="#FFFFFF">
<mx:TextArea id="txtP1T1" x="10" y="176" text="{placeName}" width="210" textAlign="center" color="#DC0000" restrict="this is where I need some help"/>
</mx:Canvas>
I'm not sure if the restrict property will cover this or not but any help will be much appreciated.
I've now managed to get something working:
private function addEvtListnerOnPlaceText():void{
txtP1T1.addEventListener(KeyboardEvent.KEY_DOWN, onKeyEventDown);
txtP1T1.addEventListener(KeyboardEvent.KEY_UP, onKeyEventUp);
}
[Bindable]
public var tempString:String;
private function onKeyEventDown(e:KeyboardEvent):void
{
var character:String = String.fromCharCode(e.charCode);
if (e.keyCode == 13)
{
tempString = txtP1T1.text;
KeyboardEvent.KEY_UP;
}
}
private function onKeyEventUp(e:KeyboardEvent):void
{
var character:String = String.fromCharCode(e.charCode);
if (e.keyCode == 13)
{
txtP1T1.text = tempString;
}
}
The only issue now is that if you hold return开发者_如何学C down, it clears the first carriage return and then keeps adding as long as you hold it down. I need a way to stop this happening without just losing focus on the text area.
No experience with flex3, but after a cursory investigation based on the idea of using an event-driven algorithm it seems you could utilize TextEvent
s and just remove any newlines/carriage returns in the inputted text before passing to the TextArea
.
Alternatively, you may want to take a look at KeyboardEvent
s.
However, it seems that utilizing the restrict property would indeed be simplest, as you can just set it to "^\r"
and that would exclude carriage returns and only carriage returns from being entered. (Of course I'd recommend using "^\r\n"
instead, to provide overall newline protection from both carriage returns, linefeeds, and any combination of the two.)
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/TextArea.html#restrict
精彩评论