4 Replies Latest reply on Mar 21, 2007 10:53 AM by Nate Weiss

    toggleOnClick doen't work right

      I am using a ToggleButtonBar with the toggleOnClick property set to "true".
      The problem is, that once I unselect the first Button by clicking, I am not able to select it again, until I select the other one.
      Any ideas?

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:ToggleButtonBar toggleOnClick="true">
      <mx:Object label="a"/>
      <mx:Object label="b"/>

        • 1. Re: toggleOnClick doen't work right
          hupfnudel Level 1
          Seems just like another bug in the flex framework to me. I have patched ToggleButtonBar myself.
          • 2. Re: toggleOnClick doen't work right
            jfillman Level 1
            Despite the negative comments of the previous poster, this is working as desined. Per the Flex documentation, if you set toggleOnClick="true", you have to click the button a second time to de-select it, before it will allow you to select the another button. If you set it to "false", the default, it will toggle back and forth the way you are expecting.
            • 3. Re: toggleOnClick doen't work right
              hupfnudel Level 1
              jfillman is wrong in two aspects:
              first: that is not what the flash documentation says. and it would be nonsense if it did, because thats not the way flex behaves.

              second: have you read my description of the problem? it is not about selecting another button.
              try toggling one button more than one time and you will notice what is wrong.

              is the code working for anyone, when he tries to toggle the first button more than once?
              i would really like to know if anybody got it working.
              • 4. Re: toggleOnClick doen't work right
                Nate Weiss
                I just ran into this issue myself.

                In my opinion, the feature *does* behave as documented--however, that behavior is not what's desired in many cases. It sounds like both hupfnudel and I want the user to be able to select any button in the bar (this works as documented and desired). The user should be able to click whatever button is currently selected, which will cause the button to deselect, leaving no selected buttons (also works). In many applications, the user (and developer) would expect that the user could click that same item again to re-select it. This part doesn't work. If you click a button that you previously deselected, you cannot reselect it again until you first select some other item. To put it another way, you can deselect the last-touched item, but you cannot select it.

                For better or for worse, the current behavior is how the documentation reads, I believe. It states that the toggleOnClick property "Specifies whether the currently selected button can be deselected by the user." It does not say anything about re-selecting the item.

                I found that I could get the desired behavior in my application by adding a itemClick event handler, and in that handler looking at the value of:

                var selected:Boolean = Button(event.target.getChildAt(event.index)).selected

                then i do:

                if (selected == false && theCurrentValue == -1) {
                Button(this.choicesButtons.getChildAt(event.index)).selected = true

                You may be wondering what "theCurrentValue" is above. In my app, I am using databinding to bind the selectedIndex of the button bar. So, this assumes that in the mxml, you have selectedIndex="{theCurrentValue}". If you are doing that, then replace theCurrentValue above with whatever you are binding the selectedIndex to. If you are not using databinding, then you'll have to change the strategy a little bit. Unfortunately you can't look at whether the selectedIndex is -1 because the index hasn't been updated to -1 yet at the time that the itemClick event fires.

                I'm sure that there would be a way to do a very simple extension of ToggleButtonBar that also includes a retoggleOnClick attribute, and if that was true then logic similar to the above would be used. For reasons specific to my app I can't do that, so I am sticking to this somwhat hacky solution, which I'll need to add for each button bar that I want to get this behavior. No biggie in this case.

                Hope that helps someone in the future.