Modify listbox with checkboxes

MacGrubR

03-08-2015

Hello,


I'm creating a form for our company to use for equipment requests.  I have a section with checkboxes and a listbox.  The listbox is a summary field which shows the user which items they've selected and what the cost will be.  The desired behavior is for the checkbox to add an item to a listbox, and when the checkbox is unchecked it removes that item from the listbox.  Adding an item works great (ListBox.addItem("Display Text","1"), however I need help with the logic for removing the item from the listbox.  Initially I tried ListBox.deleteItem(x), but it doesn't work the way I need it to.  I think the issue is deleting items by their index number.  For example:

Objects:

ListBox2

CheckboxA

if (CheckboxA.rawValue != 0 ){

  ListBox2.addItem("Chair - $200","0");

  }else {

  ListBox2.deleteItem(0);

  }

CheckboxB

if (CheckboxB.rawValue != 0 ){

  ListBox2.addItem("Desk- $300","1");

  }else {

  ListBox2.deleteItem(1);

  }

CheckboxC

if (CheckboxC.rawValue != 0 ){

  ListBox2.addItem("Trashcan - $10","2");

  }else {

  ListBox2.deleteItem(2);

  }

The problem is if you Check CheckboxA and CheckboxB and then uncheck CheckboxA it throws the index off and the checkboxes no longer remove the right item.  Is there a way to delete an item from a listbox by using it's display value rather than index?  Is there another way of going about this?

Thanks,

Tom

Accepted Solutions (0)

Answers (1)

Answers (1)

_Bruce_Robertson

MVP

08-08-2015

Hi Tom,

You can loop though the items testing for one with a specific value, in your case "0", "1", "2" with this code (for ChecboxB)

 

if (CheckboxB.rawValue != 0 ){

    ListBox2.addItem("Desk- $300","1");

}

else {

    for (var i = 0; i < ListBox2.length; i++) {

        if (ListBox2.boundItem(ListBox2.getDisplayItem(i)) == "1") {

     ListBox2.deleteItem(i);

            break;

     }

  }

}

Regards

Bruce