Listbox Checkbox Delete?

Hey all!

I am trying to figure out how to use checkbox’s to determine which row is selected and then a pushbutton to remove the selected rows.

at the moment I have only gotten as far as removing the first selected row using the following code in a pushbuttons action event.

if listbox1.CellCheckBoxValueAt(0, 1) = true then
  listbox1.Removerow(0)
end if

I know it is behaving as expected as I’m telling it to remove row 0. my problem is how do I delete all checked rows?

I am assuming some sort of loop is needed? but this is beyond my codding knowledge at this point.

In my window open event I have:

Listbox1.ColumnTypeAt(1) = ListBox.CellTypes.CheckBox

listbox1.addrow("Test1")
listbox1.addrow("Test2")
listbox1.addrow("Test3")
listbox1.addrow("Test4")
listbox1.addrow("Test5")
listbox1.addrow("Test6")
listbox1.addrow("Test7")
listbox1.addrow("Test8")
listbox1.addrow("Test9")
listbox1.addrow("Test10")
listbox1.addrow("Test11")

This is what my window looks like

CheckBoxTest1

If anyone can provide guidance or point me in the right direction to achieve what I’m intending to do, it is much appreciated.

Robin

Not tested, typed in the forum:

Var ListBoxRowCount As Integer

ListBoxRowCount = LisrtBox1.RowCount

For i = 0 To ListBoxRowCount
if listbox1.CellCheckBoxValueAt(i, 1) = true then
  listbox1.Removerow(i)
end if
Next

Check for typo.

Warning: Jeff is right, the removal must be done in a backward loop ! (bad memory)

1 Like

Something like this: (this is API1 code)

note that it goes down… if you go up, then deleting a row changes the row number and will cause issues



for loopx as integer = mylist.listcount-1 downto 0

  
  Try
    If mylist.cellstate(loopx,1) = CheckBox.CheckedStates.Checked Then
      myslist.remove loopx
    end if
    catch
end try
next
1 Like

@Emile_Schwarz and @Jeff_Tullin

Thank you both!

That did the trick Jeff.