Ever wonder how you could create an Alert control but make the text be non-selectable (without subclassing the Alert control, that is)? Well, here’s how.

By using the hidden powers of the “mx_internal” namespace, you can access the Alert control’s internal alertForm property (an AlertForm object — for more information see the AlertForm.as class in the mx.controls.alertClasses package) and from there modify the internal textField property (a UITextField object), and set the selectable property to false.

Taa-daa! Non-selectable text.

Full code after the jump.

Since this example uses the mx_internal namespace, you can’t always depend on this behavior to work in future versions of the Flex SDK. Use at your own risk.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/10/creating-an-alert-control-with-non-selectable-text/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init()">

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

            private var a:Alert;

            private function init():void {
                Alert.buttonWidth = 100;
                Alert.okLabel = "You rule!";
                Alert.cancelLabel = "I hate you!";
                a = Alert.show("This is some text, but you cant select it.", "An Alert control with unselectable text", Alert.OK | Alert.CANCEL);
                a.mx_internal::alertForm.mx_internal::textField.selectable = false;
            }
        ]]>
    </mx:Script>

    <mx:Button label="Launch Alert" click="init()" />

</mx:Application>

View source is enabled in the following example.

 
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.

0 Responses to Creating an Alert control with non-selectable text

  1. Campbell says:

    Isnt stepping into the mx_internal liable to break with with Framework revisions? especially with Flex 3 becoming open source? Just thought it might pay to note that to people.

    I have done it before too though, to get around things :)

  2. peterd says:

    Campbell,

    Good point. You’re on your own, peoples! Neither I nor Adobe guarantees that this trick/hack will work in any future revisions, so use at your own risk.

    Thanks for keeping me honest! I think I had a disclaimer like that on a previous post when I used mx_internal. I’ll see if I can find my wording and put a more official-y note above. [Update: Done!]

    Happy Flexing! Play safe!

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