9 Replies Latest reply on Jul 29, 2009 2:17 PM by Gregory Lafrance

    Unable to set selected index property of a ToggleButtonBar to "0"

    kminKeller-si5I0P

      Hi:

       

      I am having issue with ToggleButtonBar.

       

      I have set my toggleButtonBar with a viewStack with list of VBoxes which contains formItems. When the page loads for the firsttime, everything is fine. THe first button on the toggleButtonBar is selected with the correct formItem from the viewstack. I fill out the forms on each section and hit the submit button. It will take me to the home page again. Next time when I click on a button to take me to the toggleButtonBar, the last selected button is selected with the form associated to that. I need to reset the selection of the togglebuttonBar to the first one. I tried to set the selectedIndex of both toggleButtonBar and the viewStack to "0", but doesnot work.

       

      Anyone immediate help would be very highly appreciated.

        • 1. Re: Unable to set selected index property of a ToggleButtonBar to "0"
          Gregory Lafrance Level 6

          What you are describing should work. Please post simplified yet complete sample code and an answer will be soon forthcoming I am sure.

          • 2. Re: Unable to set selected index property of a ToggleButtonBar to "0"
            kminKeller-si5I0P Level 1

            Sorry when i created a sample test out from my project, it works fine. So I dont know what I am doing wrong. When I try to trace my project, the init() function  where I have set the selectedIndex=0 doesnot gets called the second time it loads and hence the index is not set back. It gets called the first time when I first load the page.

            • 3. Re: Unable to set selected index property of a ToggleButtonBar to "0"
              Gregory Lafrance Level 6

              If you are calling init() using the creationComplete or initialize events, you may need to change to the show event.

               

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

              • 4. Re: Unable to set selected index property of a ToggleButtonBar to "0"
                kminKeller-si5I0P Level 1

                Thanks for the response Greg.

                 

                Here is how my code looks:

                 

                <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" show="init()">

                <mx:Script>

                <![CDATA[

                private function init(): void
                {
                smViewStack.selectedIndex = 0;
                toggleButtonBar.selectedIndex = 0;
                }
                ]]>

                </mx:Script>
                <mx:VBox width="100%"
                   verticalAlign="middle" styleName="toggleButtonBarTrackBG">
                   <mx:ToggleButtonBar id="toggleButtonBar"
                            dataProvider="{smViewStack}"/>
                </mx:VBox>

                 

                   <mx:ViewStack id="smViewStack" width="100%" height="100%"
                     paddingBottom="5" paddingTop="5" paddingLeft="5" paddingRight="5"
                     styleName="podBG">
                       
                        <mx:VBox label="Basics" width="100%" height="100%"
                         showEffect="Fade" hideEffect="Fade"
                         styleName="innerContainerBG">
                            <basics:Basics />
                        </mx:VBox>

                        <mx:VBox label="Details" width="100%" height="100%"
                         showEffect="Fade" hideEffect="Fade"
                         styleName="innerContainerBG">
                            <details:DetailsAccordion />
                        </mx:VBox>

                        <mx:VBox label="Awards" width="100%" height="100%"
                         showEffect="Fade" hideEffect="Fade"
                         styleName="innerContainerBG">
                           <awards:AwardsView />
                        </mx:VBox> 
                        
                        <mx:VBox label="Approvals" width="100%" height="100%"
                         showEffect="Fade" hideEffect="Fade"
                         styleName="innerContainerBG">
                           
                        </mx:VBox>  
                       
                        <mx:VBox label="Payments" width="100%" height="100%"
                         showEffect="Fade" hideEffect="Fade"
                         styleName="innerContainerBG">
                           
                        </mx:VBox>
                       
                        <mx:VBox label="Reports" width="100%" height="100%"
                         showEffect="Fade" hideEffect="Fade"
                         styleName="innerContainerBG">
                           
                        </mx:VBox>
                       
                    </mx:ViewStack>
                </mx:VBox>

                 

                adding show event didnt work either. The trace doesnot even go into this init() function.

                • 5. Re: Unable to set selected index property of a ToggleButtonBar to "0"
                  Gregory Lafrance Level 6

                  After the data has been submitted, call the init method from there.

                   

                  If this post answered your question or helped, please mark it as such.

                  • 6. Re: Unable to set selected index property of a ToggleButtonBar to "0"
                    kminKeller-si5I0P Level 1

                    Sorry Greg, I am a new to Flex. When you say call the method frmo there, did you mean from a funtion that gets call from a click of the submit button? BTW My button is in a separate page. How can I reference to this method from that page? Please let me know. Thanks.

                     

                    below is my code where submit button is

                     

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
                    width="250" height="100%"
                    backgroundColor="#CCCCCC">


                    <mx:Script>
                      <![CDATA[
                      private function submitFormItem():void
                      {
                       ......
                       ......
                       // I insert form data into the database.
                       // and redirect to homepage.
                      }
                      ]]>
                    </mx:Script>

                    <mx:Spacer height="100%"/>
                    <mx:HBox>
                      <mx:Button label="Submit" click="submitFormItem()" />
                    </mx:HBox>
                    </mx:VBox>

                    • 7. Re: Unable to set selected index property of a ToggleButtonBar to "0"
                      Gregory Lafrance Level 6

                      Well, you need to know how to communicate between the main app and components. It is best to use custom events, but if you are new to Flex that might be confusing, so you can use mx.core.Application.application.

                       

                      So if your submit button is in a component myComp1 in the main app, and if your ToggleButtonBar is in another component myComp2 in the main app, then in the click handler:

                       

                      mx.core.Application.application.myComp2.myTogButBar.selectedIndex=0;

                       

                      You app and components are probably structured differently, but you should see what's going on here.

                       

                      If this post answered your question or helped, please mark it as such.

                      • 8. Re: Unable to set selected index property of a ToggleButtonBar to "0"
                        kminKeller-si5I0P Level 1

                        Thanks Greg. I will learn to use custom events later. FOr now I got it fixed using the mx.core.Application.application.myComp2.myTogButBar.selectedIndex=0 way.

                         

                        Thanks a lot and i really appreciate your help.

                        • 9. Re: Unable to set selected index property of a ToggleButtonBar to "0"
                          Gregory Lafrance Level 6

                          Glad to help. Maybe my post being the answer is not showing up yet, but if you forgot to mark my poist as the answer, it would be appreciated.