Changing the default button labels on an Alert control

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>

[GoogleAdsWide]

View source is enabled in the following example.

8 thoughts on “Changing the default button labels on an Alert control

  1. 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!

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

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

Comments are closed.