Changing the default button labels on an Alert control

by Peter deHaan on July 25, 2007

in Alert

When I first started playing with the Alert control, this tripped me up for a couple minutes. When trying to override the default text on the Alert control’s buttons, you need to set the Alert.cancelLabel, Alert.noLabel, Alert.okLabel, and/or Alert.yesLabel static properties *before* calling the Alert.show(). Sure, sounds a bit obvious in hindsight, but it is also important to remember that since those values are static, they apply to all Alerts, not just the one specific Alert. So you may have to reset the values back to their defaults once the Alert has been displayed.

Full code after the jump.

The following example displays an Alert dialog and overrides the default values for the Alert.noLabel and Alert.yesLabel properties:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/07/25/changing-the-default-button-labels-on-an-alert-control/ -->
<mx:Application name="Alert_noLabel_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="showAlert()">

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

            private function showAlert():void {
                Alert.noLabel = "Non";
                Alert.yesLabel = "Oui";

                Alert.show("message goes here",
                        "title goes here",
                        Alert.NO | Alert.YES);
            }
        ]]>
    </mx:Script>

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

</mx:Application>

View source is enabled in the following example.

{ 7 comments… read them below or add one }

1 virat August 1, 2007 at 10:15 pm

it’s very helpful when uwant to dispaly your indication in alert message….

Reply

2 ecspace January 13, 2008 at 12:47 pm

good function for alert box

Reply

3 memo January 31, 2008 at 3:46 am

wau, UR genius

Reply

4 Pablo Apanasionek February 7, 2008 at 11:02 am

Any idea on how to deal with strings being clipped when they are too long for the default button length?

Great blog for the daily morning reading!

Reply

5 Ram August 28, 2008 at 5:58 am

Wat for you are using this man ?
“private var a:Alert;”

Also I think after this method call all the alert boxex will be defaulted to this?

Reply

6 peterd August 28, 2008 at 7:01 am

Ram,

The “private var a:Alert;” isn’t used at all (oops!)
I’m guessing at one point I was thinking of saving a reference to the Alert, but then didn’t, and forgot to clean up the code.

I’ll fix the sloppy code and upload a SWF-n-stuff. Thanks for the heads-up!

Peter

Reply

7 Boris September 10, 2008 at 12:43 am

To Pablo Apanasionek :
From long string in alert’s button, you can set the static property : Alert.buttonWidth.

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: