7 Replies Latest reply on Feb 17, 2008 9:47 AM by Gregory Lafrance

    Component in an alert?

    mac_55 Level 1
      Hi,

      I like the style and functionality that an Alert box gives me. I was wondering if it's possible to add button components to the alert box instead of the usual 'Ok' button?

      Thanks
        • 1. Re: Component in an alert?
          Ratsnackbar Level 2
          You will want to read the documentation on the Alert to know what all it can do. But the answer is Yes you can add more buttons to the Alert. An example of this is this piece of code from one of my apps.

          Alert.show("Are you sure you want to delete the menu item "+currentMenuItem.menuItemLabel+"?","Delete Menu Item?",Alert.YES | Alert.NO,this,deleteResultHandler);

          In my application this changes the default OK button to Yes and No buttons and when one of them is clicked it calls the deleteResultHandler to process the selection and do whatever I need to base on the selection.

          If you were wanting to add more functionality then that however I would suggest possibly not using alert but using the PopUpManager and a custom control skinned to look like an alert instead. You can still make it modal and add features such as Text boxes for Feedback and what not.
          • 2. Re: Component in an alert?
            mac_55 Level 1
            Thanks,

            I've set my alert up according to your code and it's looking good. Do I need to make the handler take a special type of event? I guess I need to be able to tell which button was pressed in the event handler.
            • 3. Re: Component in an alert?
              ntsiii Level 3
              No, see the docs, it explains how to determine which button is clicked.
              Tracy
              • 4. Component in an alert?
                Ratsnackbar Level 2
                Here this is my handler.

                private function deleteResultHandler(event:CloseEvent):void{
                if(event.detail == 1)
                {
                //Do something here related to the button clicked.
                }
                }

                I use the event.detail to check the button so I needed the event:CloseEvent.
                • 5. Re: Component in an alert?
                  Gregory Lafrance Level 6
                  A more complete example:

                  <?xml version="1.0"?>
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
                  <mx:Script>
                  <![CDATA[
                  import mx.controls.Alert;
                  import mx.events.CloseEvent;

                  private function alertListener(eventObj:CloseEvent):void {
                  switch(eventObj.detail) {
                  case(Alert.YES):
                  myText.text = myInput.text;
                  Alert.show("Text was copied!");
                  break;
                  case(Alert.NO):
                  myText.text = myInput.text;
                  Alert.show("Text was NOT copied!");
                  break;
                  case(Alert.CANCEL):
                  myText.text = myInput.text;
                  return
                  }
                  }
                  ]]>
                  </mx:Script>
                  <mx:TextInput id="myInput" width="150" text="" />
                  <mx:Button id="myButton" label="Copy Text"
                  click='Alert.show("Do you want to copy the text?", "Alert",
                  Alert.YES | Alert.NO | Alert.CANCEL, this,
                  alertListener, null, Alert.OK);'/>
                  <mx:TextInput id="myText"/>
                  </mx:Application>
                  • 6. Re: Component in an alert?
                    mac_55 Level 1
                    Thanks guys, that's brilliant...

                    I'm now using that and it's prefect for what I'm doing..except I'm unsure how to pass other variables to the function with the closeevent..

                    e.g. change
                    private function alertListener(eventObj:CloseEvent):void {

                    to something like

                    private function alertListener(eventObj:CloseEvent, string1:String):void {

                    and how do I call it?

                    I changed this...

                    <mx:Button id="myButton" label="Copy Text"
                    click='Alert.show("Do you want to copy the text?", "Alert",
                    Alert.YES | Alert.NO | Alert.CANCEL, this,
                    alertListener, null, Alert.OK);'/>

                    to...

                    <mx:Button id="myButton" label="Copy Text"
                    click='Alert.show("Do you want to copy the text?", "Alert",
                    Alert.YES | Alert.NO | Alert.CANCEL, this,
                    alertListener(event, myString), null, Alert.OK);'/>

                    but not much luck.
                    • 7. Re: Component in an alert?
                      Gregory Lafrance Level 6
                      You can define your alertListener as you indicated:

                      private function alertListener(eventObj:CloseEvent, string1:String):void {
                      ...
                      'Alert.show("Do you want to copy the text?", "Alert",
                      Alert.YES | Alert.NO | Alert.CANCEL, this,
                      null, Alert.OK);
                      }

                      And then show the alert from that function:

                      <mx:Button id="myButton" label="Copy Text"
                      click="alertListener(event, myString)"/>