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.

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

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

 
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.

8 Responses to Changing the default button labels on an Alert control

  1. virat says:

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

  2. ecspace says:

    good function for alert box

  3. memo says:

    wau, UR genius

  4. Pablo Apanasionek says:

    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!

  5. Ram says:

    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?

  6. peterd says:

    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

  7. Boris says:

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

  8. Rajesh says:

    Nice prank…:) thanks…

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