Removing containers from canvas

What am I doing wrong?

I am following the DownloadContainer example project. I have a canvas where I embed several iterations of a container control using a property mRows(-1). When I remove, the first one in the list ( mRows(0) ), then remove the now first item in the list, I end up with the index of that item being 1 instead of 0. My understanding of arrays is that if you have an array of say 10 items (0-9) and you remove the item with index 5, items 6-9 now are 5-8 since .Ubound will return 8. Hopefully I am explaining the scenario sufficiently.

[code] Dim offset As Integer

if mRows.Ubound > -1 then
if idx > -1 and idx <= mRows.Ubound then
offset = (mRows.Ubound -1) * mRows(mRows.Ubound).Height
ScrollBar1.Maximum = offset

  mRows(idx).Close
  mRows.Remove(idx)
  
  if mRows.Ubound > 0 then
    for i As Integer = idx to mRows.Ubound
      mRows(i).Top = mRows(i).Top - mRows(i).Height
    Next
  end if
end if

end if

ScrollBar1.Maximum = offset[/code]

You got it right. The array is reordered when you remove an element. That is why if you want to remove all elements, you should to it from top to bottom.

I found the hole in my thinking. When I add (embed) the container in the canvas control, I place mRows.Ubound into a property which allows me to reference the mRows() item. However, when you remove an item, the index property on my container control is not updated (obviously).

So a more accurate question would be: How do I retrieve the actual index # (mRows) from the CC if I return c As ccItem? All I can think of is to loop through the remaining CCs and manually update the Index property. There has to be a more efficient way of doing it.

[quote=190077:@Scott Rich]I found the hole in my thinking. When I add (embed) the container in the canvas control, I place mRows.Ubound into a property which allows me to reference the mRows() item. However, when you remove an item, the index property on my container control is not updated (obviously).

So a more accurate question would be: How do I retrieve the actual index # (mRows) from the CC if I return c As ccItem? All I can think of is to loop through the remaining CCs and manually update the Index property. There has to be a more efficient way of doing it.[/quote]

Indeed a control set is not an array, so if you remove an element, it won’t reorder.

So manual update it is.

for i As Integer = 0 to mRows.Ubound mRows(i).Index = i next

Does IndexOf work in this case? You should be able to find the instance in the array with

dim i as integer = mRows.IndexOf (me)