RemoveRow Problem

Hi,
I’m dragging rows from one listbox to another. After the drag I want to remove the rows from the listbox they were dragged from. My code works fine for one row, but when two rows are selected, it will only remove the first selected row. When three or four rows are selected, it removes the first and third row only.

In the originating listbox’s DragRow event I have this:

[code] Redim aRowsToRemoveAfterDrag(-1)

Dim nRows, i as Integer
nRows=Me.ListCount-1
For i=0 to nRows
If Me.Selected(i) then
Drag.AddItem(0,0,20,4)
Drag.Text=Me.List(i) //get text. i is the row index. gets the first columns data
aRowsToRemoveAfterDrag.Append i // Remember which rows to remove
End if
Next
Return True //allow the drag[/code]

In the receiving listbox’s DropObject event I have this:

[code] Do
If Obj.TextAvailable then // Bail out if dropped object is vacant

  dim row, col, i, x as Integer
  
  Dim xValue As Integer
  xValue = System.MouseX - Me.Left - Self.Left // Calculate current mouse position relative to top left of ListBox
  
  Dim yValue As Integer
  yValue = System.MouseY - Me.Top - Self.Top // Calculate current mouse position relative to top of ListBox.
  
  row = Me.RowFromXY(xValue, yValue)
  col=Me.ColumnFromXY(xValue, yValue)

  msgBox( "You dropped on  row " + str( row ) + ", column " + str( col ) )
  
End if

Loop until Not obj.NextItem

// Remove dragged rows from original listbox
dim i as integer
for i = 0 to ubound( aRowsToRemoveAfterDrag )
listListings.RemoveRow( aRowsToRemoveAfterDrag( i ) )
next[/code]

Many Thanks!

// Remove dragged rows from original listbox
dim i as integer
for i = ubound( aRowsToRemoveAfterDrag ) downto 0
listListings.RemoveRow( aRowsToRemoveAfterDrag( i ) )
next

reverse the loop… otherwise you will not remove all the rows, and usually remove rows not intended…

Remember, the instant that you delete a row, all the rows below it have their row number decremented by 1. That’s why you want to code it as Dave shows.

Thanks guys!