10 Replies Latest reply on Jun 30, 2009 2:53 PM by Schlamazel

    Remote call to a button

    Schlamazel Level 1

      Is it possible to call button click event remotely from a change in the text input?

       


              <![CDATA[
                  import spark.events.TextOperationEvent;
                  import mx.controls.Alert
                  protected function button1_clickHandler(event:MouseEvent):void
                  {
                      Alert.show ("Hello World")
                  }

       

       

       

                  protected function textarea1_changeHandler(event:TextOperationEvent):void
                  {
                      // HOW TO CALL THE ABOVE BUTTON CLICK FROM HERE?
                  }

       

              ]]>
          </fx:Script>

       

      Thank you,

       

      GT

        • 1. Re: Remote call to a button
          oldMster Level 3

          Unless you specifically need data in the MouseEvent type event to do the button1_clickHandler(event...., change it's expected event type to plain old Event, then you can call the function from the textarea function:

           

                  <![CDATA[
                      import spark.events.TextOperationEvent;
                      import mx.controls.Alert
                      protected function button1_clickHandler(event:Event):void
                      {
                          Alert.show ("Hello World")
                      }

           

           

           

                      protected function textarea1_changeHandler(event:TextOperationEvent):void
                      {
                          // HOW TO CALL THE ABOVE BUTTON CLICK FROM HERE?

                         button1_clickHandler(event);
                      }

           

                  ]]>
              </fx:Script>

          • 2. Re: Remote call to a button
            *Prashant Shelke* Level 4

              protected function textarea1_changeHandler(event:TextOperationEvent):void
                        {
                           button1.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
                        }

            • 3. Re: Remote call to a button
              EgorKDie Level 2

              If you don't need the event object, you can make it even easier than that!

               

              <![CDATA[
                          import spark.events.TextOperationEvent;
                          import mx.controls.Alert
                          protected function button1_clickHandler(event:MouseEvent = null):void
                          {
                              Alert.show ("Hello World")
                          }

               

               

               

                          protected function textarea1_changeHandler(event:TextOperationEvent):void
                          {
                              // HOW TO CALL THE ABOVE BUTTON CLICK FROM HERE?

                             // LIKE THIS!

                             button1_clickHandler();
                          }

               

                      ]]>
                  </fx:Script>

               

              note the "= null" in the click handler, this defaults the value of the event to null if you don't specify one yourself, so you can just leave it out when you call it!

              • 4. Re: Remote call to a button
                Schlamazel Level 1

                Great! This works. Thank you!

                • 5. Re: Remote call to a button
                  EgorKDie Level 2

                  Mark one of us as correct then! We are all dying to know which way you chose to do it

                  • 6. Re: Remote call to a button
                    Schlamazel Level 1

                    *This works! *

                     

                     

                    <?xml version="1.0" encoding="utf-8"?>

                     

                    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://

                    ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo"

                    minWidth="1024" minHeight="768">

                     

                     

                    <fx:Script>

                     

                     

                    <![CDATA[

                     

                    import spark.events.TextOperationEvent;

                     

                    import mx.controls.Alert

                     

                    protected function button1_clickHandler(event:MouseEvent):void

                     

                    {

                     

                    Alert.show ("Hello World")

                     

                    }

                     

                     

                     

                     

                    protected function textarea1_changeHandler(event:TextOperationEvent):void

                     

                    {

                     

                    // Call the above button click

                     

                    button1.dispatchEvent(new MouseEvent(MouseEvent.CLICK));

                     

                    }

                     

                     

                    ]]>

                     

                    </fx:Script>

                     

                     

                    <s:Button x="155" y="138" label="Button"

                    click="button1_clickHandler(event)" id="button1"/>

                     

                    <s:TextArea x="101" y="184" change="textarea1_changeHandler(event)"/>

                     

                     

                     

                    </s:Application

                    • 7. Re: Remote call to a button
                      martinjconnolly Level 1

                      couldn't you just do this?

                       

                      protected function showMessage():void {

                      Alert.show ("Hello World")

                      }

                       

                      protected function button1_clickHandler(event:MouseEvent):void

                       

                      {

                       

                      showMessage();

                       

                      }

                       

                       

                       

                       

                      protected function textarea1_changeHandler(event:TextOperationEvent):void

                       

                      {

                       

                      // Call the above button click

                       

                      showMessage();

                       

                      }

                      • 8. Re: Remote call to a button
                        oldMster Level 3

                        If all that was desired was a simple task that didn't need any values from the actual event, then yes, that would work.  Since more complex actions are probably the actual desired result, the answer marked as the 'correct' answer is the best answer provided, because it eliminates duplication of code, and still provides full access to the Mouse 'Click' event, whichever method of firing the function is used.

                        Mark

                        • 9. Re: Remote call to a button
                          *Prashant Shelke* Level 4

                          Your wel-come

                          • 10. Re: Remote call to a button
                            Schlamazel Level 1

                            I understand. Thank you.

                             

                            Glenn