3 Replies Latest reply on Sep 5, 2009 2:00 AM by Andrew Rosewarn

    problem in dispatchevent?

    435.mahesh

      Here i have pasted the code.
      Test2.mxml
      --------------------------
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="714" height="380" >
           <mx:Script>
         <![CDATA[

      private function addBar():void {
            
            dispatchEvent(new Event('addButton'));
      }

      ]]>
      </mx:Script>

      <mx:Metadata>
        [Event(name="addButton", type="flash.events.Event")]
      </mx:Metadata>

      <mx:LinkButton color="red" x="147" y="261" label="A" click="addBar()"/>


      </mx:Canvas>

      Test.mxml
      -----------------
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="880" height="484" xmlns:components="components.*

      " xmlns:ns1="*">
          <mx:Script>
      <![CDATA[
      import mx.controls.Button;
      import mx.controls.Alert;
      private function addButtonHere():void {    
          
      var linkButton:Button =new Button();
          linkButton.label = 'A';
          cBar.addChild(linkButton);
      }
      ]]>
         </mx:Script>
      <mx:ApplicationControlBar id="cBar" width="763" height="42">
      <mx:Button label="settings"/>
      <mx:Button  label="settings1"/>
      </mx:ApplicationControlBar>
      <components:Test2 x="49" y="64" addButton="addButtonHere()">
      </components:Test2>
      </mx:Canvas>
      Main1.mxml(application)
      -------------------------------------
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:components="components.*" width="1064" height="600"> 
          
          <components:Test />
      </mx:Application>
         Here My requirement is i have one more linkbutton named 'B' in Test2.mxml.When i clicked on B it will be
      added to applicationcontrolbar(Test.mxml).How can i achieve this?
      plz help me
      --
      Regards
      D.Mahesh Babu

        • 1. Re: problem in dispatchevent?
          Andrew Rosewarn Level 3

          Hi

           

          The easiest way to add your new button is just to create a new event and handler as below

           

          Test.mxml

           

          <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="880" height="484" xmlns:components="components.*

          " xmlns:ns1="*">

              <mx:Script>

          <![CDATA[

          import mx.controls.Button;

          import mx.controls.Alert;

           

          private function addButtonAHere():void {   

              var linkButton:Button =new Button();

              linkButton.label = 'A';

              cBar.addChild(linkButton);

          }

           

          private function addButtonBHere():void {   

              var linkButton:Button =new Button();

              linkButton.label = 'B';

              cBar.addChild(linkButton);

          }

          ]]>

             </mx:Script>

          <mx:ApplicationControlBar id="cBar" x="10" y="10" width="763" height="42">

          <mx:Button label="settings"/>

          <mx:Button  label="settings1"/>

          </mx:ApplicationControlBar>

           

          <ns1:Test2 x="40" y="60" addButtonA="addButtonAHere()" addButtonB="addButtonBHere()">

          </ns1:Test2>

           

           

          </mx:Canvas>

           

          Test2.mxml

           

          <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="714" height="380" >

               <mx:Script>

             <![CDATA[

           

          private function addBarA():void {

                 dispatchEvent(new Event('addButtonA'));

          }

           

          private function addBarB():void {

          dispatchEvent(new Event('addButtonB'))

          }

           

          ]]>

          </mx:Script>

           

          <mx:Metadata>

            [Event(name="addButtonA", type="flash.events.Event")]

            [Event(name="addButtonB", type="flash.events.Event")]

          </mx:Metadata>

           

          <mx:LinkButton color="red" x="46" y="53" label="A" click="addBarA()"/>

          <mx:LinkButton x="109" y="53" label="B" click="addBarB()"/>

           

           

          </mx:Canvas>

          And the application can stay the same.
          Andrew

          • 2. Re: problem in dispatchevent?
            435.mahesh Level 1

            Here I have Ten linkButtons with different labels in Test2.mxml,then can i go this procedue?

            Or is thr another way to do this?

             

             

             

             

             

             

             

             

            Regards

            D.Mahesh Babu

            • 3. Re: problem in dispatchevent?
              Andrew Rosewarn Level 3

              I'm not sure what our trying to achive but I guess it would;t be a bad idea to.

               

              Create a custom event that contains a paremeter for the new link button label and any other data.

              From each of the 10 link buttons call one function to dispatch this custom event and send the label of the clicked button - event.currentTarget.label

              Then have one function in your parent container to create the new link button based on custom event parameters received and add the new button.

               

              Thats not really real world, it would just make your example a bit easier to maintain.  Event better if you called the click event off of a parent container that the buttons were in then it would keep working as you added more and more buttons

               

              Andrew