The following example shows how you can close an Alert control by calling the PopUpManager class’s removePopUp() method.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/22/programmatically-removing-an-alert-using-the-popupmanager/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

            private var alert:Alert;

            private function showAndHide(delay:Number):void {
                var alertText:String = "I'm an Alert control. I'll disappear in " + (delay / 1000).toFixed(1) + " seconds.";
                var alertTitle:String = "Timed Alert";
                alert = Alert.show(alertText, alertTitle);
                setTimeout(hideAlert, delay);
            }

            private function hideAlert():void {
                PopUpManager.removePopUp(alert);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Launch alert"
                click="showAndHide(3000);" />
    </mx:ApplicationControlBar>

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

4 Responses to Programmatically removing an Alert using the PopUpManager

  1. yo says:

    Hi dude,
    Thanx a ton for all your posts, specially alert related. It is really very helpful for newbies and lazy people like me :). Very Good job, keep it up

  2. matias says:

    Hi,

    There is a way to do something like this, but, without the “ok” botton?
    Thanks

  3. Peter deHaan says:

    matias,

    Instead of using an Alert control, you could experiment with a Panel/TitleWindow container instead of an Alert control.

    Peter

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