7 Replies Latest reply on Feb 26, 2010 11:31 AM by sandc666-YGfr5p

    Passing Event in to a component

    sandc666-YGfr5p

      Hi

         I am new to Flex and folowing a number of book befor a course in 2 weeks, I have created a main appliciotn with function and can handle events

       

       

       

      private functionfillLabel(evt:Event):void{

       

      myLable.text = evt.target.label;

      myButton2.addEventListener(MouseEvent.CLICK,fillLabel);

       

       

      }

       

       

      ]]>

       

      </mx:Script>

       

       

       

      <mx:Panel x="320" y="127" height="200" width="250" layout="absolute" title="Testing events" >

       

       

       

      <mx:Label x="55" y="45" id="myLable" />

       

       

      <mx:Button label="text1" id="myButton" x="90" y="96" click="fillLabel(event)" />

       

      <mx:Button label="text2" id="myButton2" x="90" y="126" />

       

      </mx:Panel>

       

       

      I have also creeate a component, and passed   custom events form the component to the main appliciotn and handled the event

       

      I can pass data values [Bindable]  in to the  "myButtonComponet"

       

      so that is out of the componet "myButtonComponent"  in to the recevine component, all good an well.

       

      so compoinent "myButtonComponent"  now has a public function that pops up a message, nothing complex

       

      the problems is

       

      how would I make "myButton"  pass the event in to  "myButtonComponet" to make the function work

       

      i.e. linking the click event of "mybuttomn" toi make the Componet "myButtonComponet" do some work

        • 1. Re: Passing Event in to a component
          pauland Level 4

           

          <mx:Button label="text1" id="myButton" x="90" y="96" click="fillLabel(event)" />

           



           

          how would I make "myButton"  pass the event in to  "myButtonComponet" to make the function work

           

          i.e. linking the click event of "mybuttomn" toi make the Componet "myButtonComponet" do some work

           

          <mx:Button ...   click="myButtonComponent.someFunction()" />

           

          The button handles the click event and will be able to call a public function in the component.

           

          Alternatively your component can add an event listener to the button, but it has to know about the button to do so.

           

          Paul

          1 person found this helpful
          • 2. Re: Passing Event in to a component
            sandc666-YGfr5p Level 1

            Paul

                   that helps to call the function in the component, in button2

             

             

            what I was trying to do was to click on  button1, do somethiung, then tell button2 to process it function

             

            another way to put this would be

             

            in compone button1

             

              this.text "aaa"

             

            button2.function.dosomething()

             

            the chlamnge is making button2 react after the ode in button1 id completed

            • 3. Re: Passing Event in to a component
              pauland Level 4

              Your just making life difficullt for yourself and trying to write code that will be confusing.

               

              If I understand you correctly,

               

              User clicks button 1. The do "something", then tell button2 to do "something else".

               

              Don't do that.

               

              Write your code like this:

               

              <mx:Button id="button1" label="button1" click="doSomething();doSomethingElse()" />

              <mx:Button id="button2" label="button2" click="doSomethingElse()" />

               

              Better still write distinct event handlers for this kind of thing..

               

              <mx:Button id="button1" label="button1" click="onButton1Click()" />

              <mx:Button id="button2" label="button2" click="onButton2Click()" />

               

              and put the code you need inside those handlers.

               

              Paul

              • 4. Re: Passing Event in to a component
                sandc666-YGfr5p Level 1

                Paul

                      I am just off to try this now !!!! , thanks

                 

                you are 100% correct and I do like to keep things simple

                 

                you said

                 

                Better still write distinct event handlers for this kind of thing..

                 

                <mx:Button id="button1" label="button1" click="onButton1Click()" />

                <mx:Button id="button2" label="button2" click="onButton2Click()" />

                 

                How would the click on button1, that would run the function "onButton1Click()"  when completed, "tell" button2 to "click" and run the function click="onButton2Click()"

                 

                 

                I need this as the user can click button2 all day, but if they click button1, then after button plays some sound, button2, then button3,4,5 etc all have to play one after each other

                • 5. Re: Passing Event in to a component
                  pauland Level 4

                  You need just to pause and think.

                   

                  I orginally suggested that your click handler was

                   

                  click = "doSomething();doSomethingElse();"

                   

                  Then I suggested using

                   

                  click="onButton1Click();"

                   

                  so you need a handler function

                   

                  public function onButton1Click():void{

                   

                  // what do you think goes here?  ;-)

                   

                  }

                   

                   

                  Paul

                  • 6. Re: Passing Event in to a component
                    sandc666-YGfr5p Level 1

                    Paul

                           Maybe a little backgroud, this is an educational program to teach dyslexics to read, leasion can have 20 words each word has it own sound, so I created  a component that defines the word and sound and the code to highligt the word, play the sound, turn the word back to black, this way I can easly layout 20 words on a page <uofs:ulable id="b11" playsound="bad.mp3" click="play()" /> this all works well (by the way theye is 12,000 words to do)

                     

                    there is a screen shot at the bottom left

                     

                    http://www.unitsofsound.net/uofs/uos.html

                     

                     

                    and a demo of the eixitng PC based app

                     

                    each group of words has a controling button, say we have 4 blocks of 5 words and 4 "controling" blocks

                     

                    when you click on the block <uofs:block id="b1" sound="into.mp3" click="playblock" /> the sound plays

                     

                    so you can click on a single word and it will play <uofs:ulable playsound="bad.mp3" click="play()" />

                     

                    ok that all works,

                     

                    the busienss logic is

                     

                    when you click on the block <uofs:block  id="b11" sound="into.mp3" click="playblock" /> it plays then,  each word must play in order, b11, b12, b13,b14 ,b14,b15 etc.

                     

                    <uofs:ulable playsound="bad.mp3" click="play()" />

                    • 7. Re: Passing Event in to a component
                      sandc666-YGfr5p Level 1

                      Paul

                            Just wanted to say thanks, I got it with your help, it was simple

                       

                       

                      b11 and b12 are compoinents with publich functions

                       

                      so now when you click on b11 or b12 they play sound as they should, when you click on B1, it call the function playblock and b11 and b12 both play, I will need to add sime timeing in so they do not both play at the same time but that is easy

                       

                       

                       

                       

                      private function playblock():void{

                       

                      b11.play()

                      b12.play()

                       

                      }

                       

                       

                      ]]>

                       

                       

                       

                      <uofs:ublock id="b1" x="216" y="196.85" click="playblock()"/>