25
Jul
07

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.

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 Responses to “Changing the default button labels on an Alert control”


  1. 1 virat Aug 1st, 2007 at 10:15 pm

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

  2. 2 ecspace Jan 13th, 2008 at 12:47 pm

    good function for alert box

  3. 3 memo Jan 31st, 2008 at 3:46 am

    wau, UR genius

  4. 4 Pablo Apanasionek Feb 7th, 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!

  5. 5 Ram Aug 28th, 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?

  6. 6 peterd Aug 28th, 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

  7. 7 Boris Sep 10th, 2008 at 12:43 am

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

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;".