Determining when the user has selected all text in a FxTextArea control in Flex Gumbo

The following example shows how you can use the selectAll event to determine when the user uses the context menu or a keyboard command (Ctrl+A/Command+A) to select all text in a Flex Gumbo FxTextArea control.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3”.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/11/24/determining-when-the-user-has-selected-all-text-in-a-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_selectAll_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Script>
        <![CDATA[
            import mx.controls.Alert;

            private function textArea_selectAll(evt:Event):void {
                Alert.show(evt.type);
            }
        ]]>
    </Script>

    <Label text="Right-click the FxTextArea control below and select 'Select All' from the context menu." />
    <FxTextArea id="textArea"
            width="100%"
            height="100%"
            selectAll="textArea_selectAll(event);">
        <content>
            <String source="data/lorem.html" />
        </content>
    </FxTextArea>

</Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/11/24/determining-when-the-user-has-selected-all-text-in-a-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_selectAll_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <Script>
        <![CDATA[
            import mx.components.FxTextArea;
            import mx.controls.Alert;
            import mx.controls.Label;

            [Embed(source="data/lorem.html",
                    mimeType="application/octet-stream")]
            private const lorem:Class;

            private var textArea:FxTextArea;

            private function init():void {
                var lbl:Label = new Label();
                lbl.text = "Right-click the FxTextArea control below and select 'Select All' from the context menu.";
                addChild(lbl);

                textArea = new FxTextArea();
                textArea.content = new lorem();
                textArea.percentWidth = 100;
                textArea.percentHeight = 100;
                textArea.addEventListener(Event.SELECT_ALL,
                            textArea_selectAll);
                addChild(textArea);
            }

            private function textArea_selectAll(evt:Event):void {
                Alert.show(evt.type);
            }
        ]]>
    </Script>

</Application>

This entry is based on a beta version of the Flex Gumbo SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Gumbo SDK.