The following examples show how you can control whether a user can select the text in a Flex TextArea control by setting the selectable property in MXML and ActionScript.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/06/toggling-whether-a-user-can-select-text-in-a-textarea-control-in-flex/ -->
<mx:Application name="TextArea_selectable_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="selectable:">
                <mx:CheckBox id="checkBox"
                        selected="true" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:TextArea id="textArea"
            selectable="{checkBox.selected}"
            text="The quick brown fox jumped over the lazy dog." />

</mx:Application>

View source is enabled in the following example.

You can also set the selectable property using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/06/toggling-whether-a-user-can-select-text-in-a-textarea-control-in-flex/ -->
<mx:Application name="TextArea_selectable_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function checkBox_change(evt:Event):void {
                textArea.selectable = checkBox.selected;
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="selectable:">
                <mx:CheckBox id="checkBox"
                        selected="true"
                        change="checkBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:TextArea id="textArea"
            text="The quick brown fox jumped over the lazy dog." />

</mx:Application>
 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

4 Responses to Toggling whether a user can select text in a TextArea control in Flex

  1. adam says:

    That’s great. But how can i make all the content of a flash/flex page selectable? I hate that you can’t simply select text and do a simple ctrl+c or right click copy. Please help. Thanks for this site and all the great tips.

  2. Stuart Ward says:

    I agree with Adam. It is very frustrating and disappointing to users that expect to be able to simply click/drag to select text (especially in a datagrid).

    I posted a question in the Flexcoders forum about how to select text in a datagrid by the standard click/drag and the answer was… that there is no property or control that will allow this. You would have to try to create some custom code to emulate it (if it’s possible at all).

    Stuart

  3. Paul says:

    Is it possible to make an item disabled and selectable at the same time?

    Thanks.

  4. Ada says:

    I am writing as a user, not a designer. I have always been able to select text (Ctrl+C) and copy said text to a Word or text doc, or Excel SSheet. I’ve now found that I can no longer do this — I’m working with a 2003 .PDF doc and am using Adobe 9.2.0.

    1. Is this related to the version at creation time, or has this always been a designer option?
    2. Does this have anything to do with my current version of Reader?
    3. How can I overcome this considerable hurdle?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree