7 Replies Latest reply on Nov 19, 2006 10:13 PM by Like2Flex

    How to get the Clicked Item of a ListBox?

    Like2Flex Level 1
      I need to get the clicked item of a listbox.
      The item can be at first selected, when I click it again, it automatically gets de-selected, but I want to get that item whenever I click it. Any idea how to do this?
      .
      This line does not work, because it gets the selected item.
      var pattern:String = event.currentTarget.selectedItem.myname;

      This one must work,but something is missing.
      Anyone can help me with this?

      marks is my ArrayCollection;
      var pattern:String =marks.getItemAt[??].myname;
        • 1. Re: How to get the Clicked Item of a ListBox?
          chris.huston.t10 Level 3
          Use:
          <mx:List itemClick="show_clicked(event)"/>

          private function show_clicked(event:ListEvent):void {
          var pattern:String = event.currentTarget.selectedItem.myname;
          }

          Vygo
          • 2. Re: How to get the Clicked Item of a ListBox?
            Like2Flex Level 1
            Hi,

            But when deselect an item as well I want to get it.

            But the code (var pattern:String = event.currentTarget.selectedItem.myname;), only gives the "selecteditem"

            any idea for my problem

            thx
            • 3. Re: How to get the Clicked Item of a ListBox?
              ntsiii Level 3
              Is there a "change" event? If not, maybe a mouseUp handler?

              Tracy
              • 4. Re: How to get the Clicked Item of a ListBox?
                chris.huston.t10 Level 3
                Interesting problem. When I click the same item again, I still get the value of the selectedItem, it is not de-selected. What does the mxml for your list look like? If you use a change event, then clicking an item a second time will not give a response, but the itemClick event seems to work for my listboxes. Can you post the code for your listbox?

                Vygo
                • 5. Re: How to get the Clicked Item of a ListBox?
                  Like2Flex Level 1
                  Here is my code. Am using this function in the itemclick event of the ListBox.

                  My ListBox is a multi-select listbox. So am selecting several items by pressing "Ctrl" key and selecting each item using click.

                  Say I have selected 4 items, when I press "Ctrl" and de-select one of the items, I want to get the itemname of the deselected item.

                  public function createMarksList(event:ListEvent):void{

                  // Alert.show(new String(event.currentTarget.selectedItem.myname));
                  // var pattern:String = event.currentTarget.selectedItem.myname;
                  // pattern = pattern += "," + pattern;
                  // text1.text = pattern;
                  }


                  <mx:List x="9" y="46" width="121" height="121" id="lstMarks" allowMultipleSelection="true" itemClick="createMarksList(event)"></mx:List>

                  So, your help is required here buddy.
                  I sitll can't figure out.

                  Thx
                  • 6. Re: How to get the Clicked Item of a ListBox?
                    chris.huston.t10 Level 3
                    I figured out a way to find a deselected item. It is a bit of a hack, but it gets the job done.

                    private var selected_indices:Array = [];
                    private function show_clicked(evt:ListEvent):void {
                    var indices:Array = evt.currentTarget.selectedIndices;
                    if (indices.length < selected_indices.length) {
                    for each (var ind:int in selected_indices) {
                    if (indices.indexOf(ind) < 0) {
                    trace("deselected index = " + ind);
                    trace("deselected item = " + evt.currentTarget.dataProvider.getItemAt(ind).myname);
                    break;
                    }
                    }
                    } else {
                    trace("newly selected = " + evt.currentTarget.selectedIndex);
                    }
                    selected_indices = evt.currentTarget.selectedIndices;
                    }

                    Does this do what you need it to?

                    Vygo
                    • 7. Re: How to get the Clicked Item of a ListBox?
                      Like2Flex Level 1
                      I got it. This works pretty well.
                      But will try your code as well when free.
                      Thx

                      var marksList:String = "";
                      var selectedMarks:Array = lstMarks.selectedItems;
                      for ( var i:int; i < selectedMarks.length; i ++) {
                      marksList = marksList + selectedMarks .mymarks;
                      }
                      text1.text = marksList;