The following code shows a very basic method for displaying an Alert dialog in a Flex application. As this is a very simple example, it doesn’t show how to properly determine which button the user clicked to dismiss the dialog box, nor does it show other techniques such as modal versus non-modal dialog boxes. I’ll try and cover those in later entries.

Full code after the jump.

The following example displays a simple Alert when the user presses the Button control. The alert has two buttons, “Yes” and “No” which the user can use to dismiss the dialog:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/wp-content/uploads/Alert_closeHandler_test/bin/srcview/index.html -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

            private var alert:Alert;

            private function showAlert():void {
                var text:String = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.nntHello World";
                var title:String = "This is the title of the Alert window";
                alert = Alert.show(text, title, Alert.YES | Alert.NO);
            }
        ]]>
    </mx:Script>

    <mx:Button label="Alert.show()" click="showAlert();" />

</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.

One Response to Using the Alert control

  1. Asli Binal says:

    Hi,
    I’m calling Alert.show with a title and a string, but I never see the title – it’s as if it gets cut off! Any ideas why my title never shows up?

    Alert.show("My text", "Title");
    

    Thanks,
    AB

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