2 Replies Latest reply on Jan 1, 2011 8:57 AM by Gregory Lafrance

    One and Only One Custom Component Should be created

    kiran7881 Level 1

      public function openSearchCOmp():void
              {
                 
              a = new SearchComp();
                  a.height=250;
                  a.width=502;
                  a.x=314;
                  a.y=106;
                  a.verticalScrollPolicy="off";
                  a.horizontalScrollPolicy="off";
                  Application.application.addChild(a);
              }

       


      As you can see that ,Upon click of a Image , i am adding a Custom Component to my Application .

       


      This is working fine , but the Problem is that , as many clciks those many Components are being created .

       

      Please tell me How can i make sure that only One component is created and added to the Application ??

       

      Thanks

        • 1. Re: One and Only One Custom Component Should be created
          flex2008 Level 3

          Assuming 'a' is not a local variable,

           

          public function openSearchCOmp():void
                  {
                      if(!a)

                         {

                                   a = new SearchComp();
                      a.height=250;
                      a.width=502;
                      a.x=314;
                      a.y=106;
                      a.verticalScrollPolicy="off";
                      a.horizontalScrollPolicy="off";
                      Application.application.addChild(a);

                         }
                          
                  }

          • 2. Re: One and Only One Custom Component Should be created
            Gregory Lafrance Level 6

            One way to do this is to wrap the component in a canvas, and then do this:

             

            if(Application.application.myCanvas.numChildren == 0){

                Application.application.myCanvas.addChild(a);

            }

             

            Of course the best way to handle this is to disable the button if the item has been added.

             

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

            http://www.chikaradev.com
            Adobe Flex Development and Support Services