1 Reply Latest reply on Jul 16, 2013 2:51 AM by try67

    Determining the index of a selected item in a ListBox

    KennethWebb Level 1

      Hello,

      I can't seem to figure out a simple/direct method to identify the index of the selected item in one list box.

      I feel like I'm missing something, but I can't seem to figure which (if any) property returns an index number if you use it with the .onChange callback for a ListBox.

      The following code accomplishes what I need but isn't particularly efficient or elegant.

      Any suggestions to duplicate this same behavior with better code would be appreciated.

      Thanks!

       

       

      var res = "palette {text: 'Example List', properties:{resizeable:true}\

      pnl: Panel{orientation: 'row',preferredSize: [400,600],\

      list1: ListBox{preferredSize: [400,550], properties:{multiselect:true,numberOfColumns:2, showHeaders:true,columnTitles: ['List 1', 'Subitem 0']}},\

      }}"

       

       

      var win = new Window(res)

      win.show()

      for (i=0; i<10;i++) {

          var row = win.pnl.list1.add('item', "Same Entry")

          row.subItems[0].text = "Subitem "+i}

       

       

      win.pnl.list1.onChange = function() {

           var tempArray = new Array()

           for (i=0; i<win.pnl.list1.items.length;i++) {

               if (win.pnl.list1.items[i].selected == true){tempArray.push(i)}

               }

          var selectedRow = win.pnl.list1.items[tempArray[0]].text

          var selectedRowSubitem = win.pnl.list1.items[tempArray[0]].subItems[0]

                    alert("Selection: "+tempArray+"\n Displayed Contents:   "+selectedRow+"\t"+selectedRowSubitem)

                   

           }