9 Replies Latest reply on Oct 11, 2010 7:56 AM by jfb00

    Get custom component name

    jfb00 Level 3

      Hi all,

      I have a canvas with few custom components like:

      <mx:Canvas id="test" width="100%" height="100%">
           <custom:Form01 id="Form01_mxml" visible="false" x="0" y="0"/>

           <custom:Form02 id="Form02_mxml" visible="false" x="0" y="0"/>

           <custom:Form03 id="Form03_mxml" visible="false" x="0" y="0"/>

           <custom:Form04 id="Form04_mxml" visible="false" x="0" y="0"/>

      </mx:canvas>

       

      From a menu I setup the visibility to true to show the component that I want.

      I would like to know what custom component is visible after a result of an event. Is this posible?

      I know I can do multiple if statements to see what component is visible or not like:

      if (Form01_mxml.visible ==  true) do something

      But I have many components and I would like to find the name dynamic.

      Thanks

       

      Johnny

        • 1. Re: Get custom component name
          Devtron Level 3

          I have done something similar. It's a pain in the rear.

           

              var _formCount:Number = 4;

           

                          for (var i:int=0; i < _formCount; i++){
                              var formObj:Object = test.getChildAt(i);
                              var myFormObject:Object = formObj.getChildByName("Form0" + (i+1).toString + "_mxml");

           

                               if (formObj.visible){

                                  Alert.show('visible true');

                               } else {

                                 Alert.show('visible false');

                               }

                          }

           

           

          the .getchild function uses a zero based index. that is why i is declared and intialized at 0, and that is why you have to increment i when you get your formObject (i+1).

           

          You could fix that by renaming your child objects to start with 0, instead of 1. That would remove the need to increment i, when referencing the formObject.

           

          This should get you in the right direction. I have 26 dynamic forms in one MXML page, and I reference them and set their values this way. My code is a little large but this is the basics.

          1 person found this helpful
          • 2. Re: Get custom component name
            Ramesh Sampenga Level 1

            Instead of visible = "false"

             

            use ViewStack

             

            <mx:Canvas id="test" width="100%" height="100%">

             

                 <mx:ViewStack id="VS" selectedIndex="{list.selectedIndex}" x="0" y="0">

             

                           <custom:Form01 id="Form01_mxml"  width="100%" height="100%"/>

                           <custom:Form02 id="Form02_mxml" width="100%" height="100%"/>

                           <custom:Form03 id="Form03_mxml"  width="100%" height="100%"/>

                           <custom:Form04 id="Form04_mxml"  width="100%" height="100%"/>

                 </mx:ViewStack>

            </mx:canvas>

             

            var mychoice:Number = 2;

            and slect VS.selectedIndex = mychoice;

             

            and get the seleced Component  as

             

                  VS.selectedChild.id     (// Form03_mxml)

             

             

             

                    

            • 3. Re: Get custom component name
              jfb00 Level 3

              Thanks for you reply and help.

              this works! now in my custom component I have a public function that I want to trigger.

              I have the componen name in string: var myComp:String = VS.selectedChild.id     (// Form03_mxml)

              how can I do that?

              myComp.init(); \\This doesn'r work.

              Rgds

               

              Johnny

              • 4. Re: Get custom component name
                BhaskerChari Level 4

                You can use (VS.selectedChild as DisplayObject).init();

                 

                Thanks,

                Bhasker

                • 5. Re: Get custom component name
                  jfb00 Level 3

                  It doesn't work.

                  1061: Call to a possibly undefined method init through a reference with static type flash.display:DisplayObject.

                  Any ideas?

                   

                  Johnny

                  • 6. Re: Get custom component name
                    BhaskerChari Level 4

                    Hi Johnny,

                     

                    Sorry try (VS.selectedChild as Container).init();

                    • 7. Re: Get custom component name
                      jfb00 Level 3

                      No luck

                      Multiple markers at this line:
                      -1061: Call to a possibly undefined method init through a reference with static type mx.core:Container.
                      -Container

                       

                      Thanks!

                       

                      Johnny

                      • 8. Re: Get custom component name
                        BhaskerChari Level 4

                        Hi Johnny,

                         

                        Try this it will work I have tested it...

                         

                        var myComp:String = VS.selectedChild.id
                        var selChild:* = myViewStack.getChildByName(myComp) as DisplayObject;

                         

                        if(selChild.hasOwnProperty("init"))
                             selChild.init();

                         

                        Try this and let me know..

                         

                        Thanks,

                        Bhasker

                         

                         

                         

                         

                         

                        • 9. Re: Get custom component name
                          jfb00 Level 3

                          You got it!

                          Thanks Man

                           

                          Johnny