0 Replies Latest reply on Jan 10, 2012 8:59 PM by havefreetime

    How can I control the tab using the Button.

    havefreetime Level 1

      1.jpg

       

      This app has 5 link buttons and TabNavigator.

      1st Tab & 2st Tab can not be removed. If I click Tab1 link button, the 1st tab is chosen.

      and I click the Tab3 link button, 3st Tab can be created and chosen.

      2.jpg

      When I click 'Close' button, the tab matching the number will be removed.

       

      I wonder that...

       

       

      <?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/mx"
            width="386" height="290" minWidth="955" minHeight="600">
      <fx:Script>
        <![CDATA[
         import spark.components.Button;
        
         private var flag_tab3:Boolean = false;
         private var flag_tab4:Boolean = false;
         private var flag_tab5:Boolean = false;
        
         private function check_Tab(num:int):void
         {
          switch(num){
           case 0: break;
           case 1: break;
           case 2: if(!flag_tab3) add_Tab(num);
             break;
           case 3: if(!flag_tab4) add_Tab(num);
             break;
           case 4: if(!flag_tab5) add_Tab(num);
             break;
          }
         tab.selectedIndex = num;
         }
         
         // adding the tab
         private function add_Tab(num:int):void
         {
          var child:NavigatorContent = new NavigatorContent();

         
          // Create the label
          var lbl:Label = new Label();
          lbl.text = "Sreen"+(num+1);
         
          // Create the button
          var btn:Button = new Button();
          btn.addEventListener("click", remove_Tab);
          btn.x = 43;
          btn.y = 110;
          btn.label = "Close";
          btn.width = 100;
          btn.height = 30;
          child.addElement(lbl);
          child.addElement(btn);
         
          tab.addChild(child);
          child.label = lbl.text;
         
          switch(num){
           case 2: flag_tab3 = !flag_tab3;
             break;
           case 3: flag_tab4 = !flag_tab4;
             break;
           case 4: flag_tab5 = !flag_tab5;
             break;    
          }
         }  
        
        
         // Remove the tab
         private function remove_Tab():void
         {
          tab.removeChildAt(tab.selectedIndex);
         
         }
        ]]>
      </fx:Script>

      <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
      </fx:Declarations>
      <s:HGroup x="24" y="10" width="342" height="25">
        <mx:LinkButton label="Tab1" click="check_Tab(0)"/>
        <mx:LinkButton label="Tab2" click="check_Tab(1)"/>
        <mx:LinkButton label="Tab3" click="check_Tab(2)"/>
        <mx:LinkButton label="Tab4" click="check_Tab(3)"/>
        <mx:LinkButton label="Tab5" click="check_Tab(4)"/>
      </s:HGroup>
      <mx:TabNavigator x="24" y="67" width="343" height="200" id="tab">
       
        <!-- Screen1 -->
        <s:NavigatorContent width="100%" height="100%" label="Screen1" id="screen1">
         <s:Label text="Screen1"
            textAlign="center"/> 
        </s:NavigatorContent>
       
        <!-- Screen2 -->
        <s:NavigatorContent width="100%" height="100%" label="Screen2" id="screen2">
         <s:Label text="Screen2"
            textAlign="center"/>
        </s:NavigatorContent>
         
      </mx:TabNavigator>
      </s:Application>

       

       

      How can I get the selectedIndex value?

      I think I can use .getElementIndex. But I don't know how to use it.

       

      I have to use IVisualElement type using getElementIndex's parameter value. But I cannot understand what is IVisualElement type.

       

      and...

       

      To complete this program, do I have to use flag value... switch...  something else...

       

      My code is very poor. Is there good idea?

       

      Pls help me.

       

      ps. sorry for my english... It's not my first language...