3 Replies Latest reply on Aug 11, 2010 6:44 AM by BhaskerChari

    Dynamically creating buttons and events. Event did not work.

    gretags

      Hi Guys,

       

      I am trying to create an application that will dynamically create buttons and events....Creating a button is not a problem but the event did not work. The code below is just simple... 1st click the button and it will create another button (dynamic).... if you click that newly created button it should Alert a test2....

       

      Does anyone knows how to implement this (That works )

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
              <![CDATA[
             
          import mx.controls.*;
             
             
              private function onLoad():void
              {
                  var btn:Button = null;
                  //
                      test2();
                      btn = new Button();
                      btn.label = 'test';
                      btn.addEventListener("click", test2);
                      this.addChild(btn);           
              }
             
              private function test2():void
              {
                  Alert.show('test', 'testing');
              }       
             
             
               ]]>
           </mx:Script>
          <mx:HBox id = 'testbox' x="0" y="154" width="100%">
              <mx:Button label="Button" click = 'onLoad();'/>
          </mx:HBox>
         
      </mx:Application>

        • 1. Re: Dynamically creating buttons and events. Event did not work.
          BhaskerChari Level 4

          Hi gretags,

           

          You forgot to specify the argument in the eventListener...thats why you are getting the runtime script error..

           

          Check the below code...Now the Alert will popup...

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
              <mx:Script>
                  <![CDATA[
                 
              import mx.controls.*;
                 
                 
                  private function onLoad():void
                  {
                      var btn:Button = null;
                      //
                          //test2();
                          btn = new Button();
                          btn.label = 'test';
                          btn.addEventListener("click", test2);
                          this.addChild(btn);           
                  }
                 
                  private function test2(event:MouseEvent):void
                  {
                      Alert.show('test', 'testing');
                  }       
                 
                 
                   ]]>
               </mx:Script>
              <mx:HBox id = 'testbox' x="0" y="154" width="100%">
                  <mx:Button label="Button" click = 'onLoad();'/>
              </mx:HBox>
             
          </mx:Application>

           

          If this post answers your question or helps, please kindly mark it as such.


          Thanks,

          Bhasker Chari

          • 2. Re: Dynamically creating buttons and events. Event did not work.
            mike_morales Level 2

            You should use the constant instead,  btn.addEventListener(MouseEvent.CLICK, test2);

            • 3. Re: Dynamically creating buttons and events. Event did not work.
              BhaskerChari Level 4

              Hi Mike,

               

              The problem is not because of using the MouseEvent.CLICK static constant....He can also pass the string literal "click" ....the MouseEvent.CLICK constant

               

              inturn refers to the string "click" only....

               

              The actual problem here is the argument which he didn't specify in the test2(event:MouseEvent) function...

               

               

              Regards,

              Bhasker Chari