Alert! Alert! Listen to the buttons!

6 Sep
2005

The question of how to handle/listen to button clicks from the Alert control pops up (excuse the pun) quite often. To do this you need to create an actionscript function to handle the event. Other situations may arise where you also need to pass further information to the handler function. This post will show you how ;-)

The mx.controls.Alert class has a static method that shows the Alert control with the title, message, and requested buttons. The following code block will demonstrate how to declare a listener for your Alert control, as well as what to do when you catch the event.

In short, the code shows how to create an Alert control which has 2 buttons, Yes and No. Keep an eye out for for line 12 – this is where we delegate the button click listener to the handleAlert method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import mx.controls.Alert;
import mx.utils.Delegate;
 
// show the Alert control
public function showAlert() : Void
{   
    // the show method follows the method signature from the Flex Actionscript API
    Alert.show( "Are you sure?",
                         "Confirm Delete", 
                         Alert.YES| Alert.NO,
                         null,
                         Delegate.create(this, this.handleAlert),
                         null,
                         Alert.YES);
}
 
// handle the Alert control button click
public function handleAlert( event:Object ) : Void
{
    if( event.detail == Alert.YES)
    {
        // YES button was clicked
    }
    else if( event.detail == Alert.NO)
    {
        // NO button was clicked
    }
}

Quite similarly we can also create an inline function, which will allow us to pass extra variables to the handler method. Take a look at the following and how it declares a function which takes an event object and then makes a call to our own handleAlert method:

1
2
3
4
5
6
7
8
//line 12 replacement
Delegate.create( this, function(event){this.handleAlert(event, "hello");} )
 
//modified alertHandler method signature
public function handleAlert( event:Object, test:String ) : Void
{
    //handle the event.detail property etc
}

You can see that the above example passes the string hello through to our handleAlert method. Don’t forget to change the method signature for the handleAlert method to compensate for the extra variable(s).

Now you can not only create, but successfully listen to and handle the button clicks from within an Alert control. Keep an eye out for another post soon where I’ll demonstrate how to further customise the Alert control, but also outline the setbacks with its current implementation :-)

Related posts:

  1. Strongly type a CF return using Cairngorm
  2. Cast a loaded Flex Application to an Interface
  3. Security – Remote Objects and Flex
  4. Waiter, there is a bug in my CFC!
  5. Building desktop applications with HTML and JavaScript


6 Responses to Alert! Alert! Listen to the buttons!

Avatar

Blog d’un développeur Flex. » Blog Archive » Différents liens intéressants

March 13th, 2007 at 10:54 am

[...] Récupérer le bouton pressé sur un mx.controls.Alert (avec un handler d’évènement, pratiqu… [...]

Avatar

shiju

December 4th, 2008 at 1:24 am

simple but usefull

Avatar

Ryan Dunn

January 16th, 2009 at 3:37 am

I found that Action Script 3 doesn’t support the –Delegate.create(this, this.handleAlert)– section of your code.

Ryan

Avatar

Stephanie

April 30th, 2009 at 9:12 am

Flex 3 has removed the Delegate class, but this would still be something usefull (unless everyone else is dont delete confirms differently then I am).
How do we accomplish the same task with Flex 3?

Avatar

jacob

May 10th, 2009 at 5:15 am

Hi there, Nice example.. but….

How does it work when i want to have a checkbox in my alert window?
Something like a Remember this setting checkbox.

And how do i get a listener from that?

Greets, Jacob

Avatar

handoyo

July 30th, 2009 at 10:52 pm

Hi all,just remove the delegate..I use this code

private function hapus(e:ContextMenuEvent):void
{
Alert.show(“Anda Yakin?”,”Hapus Data”,Alert.YES|Alert.NO,null,this.handlehapus);
}

private function handlehapus(event:CloseEvent):void
{
if (event.detail==Alert.YES)
{
Alert.show(“Warning”,”Anda milih yes”);
}
else
{
Alert.show(“Warning”,”Anda milih no”);
}

For me it works…

Comment Form

top