21
Mar
08

Creating an undraggable Alert control in Flex

The following example shows how you can create a Flex Alert control that isn’t draggable by listening for the mouseDown event and calling the stopImmediatePropagation() method in the event handler.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/21/creating-an-undraggable-alert-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

            private function draggableAlert():void {
                Alert.show("Drag me!");
            }

            private function undraggableAlert():void {
                var alert:Alert = Alert.show("Drag me!");
                alert.addEventListener(MouseEvent.MOUSE_DOWN, alert_mouseDown, true);
            }

            private function alert_mouseDown(evt:MouseEvent):void {
                evt.stopImmediatePropagation();
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Draggable Alert"
                click="draggableAlert();" />
        <mx:Button label="Undraggable Alert"
                click="undraggableAlert();" />
    </mx:ApplicationControlBar>

</mx:Application>

View source is enabled in the following example.


1 Response to “Creating an undraggable Alert control in Flex”


  1. 1 Bruno Bergher Mar 22nd, 2008 at 1:10 pm

    This is incredibly userful, for many other situations as well.
    Preventing the propagation of an event, or at least sometimes it’s default behaviour (flash.events.Event.preventDefault) is a really nice tip.

    Thanks again!

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".