First published at 20:12 UTC on January 20th, 2024.
VB.Net - adding and deleting Controls (objects) from Forms
FOR EACH control Add and Delete...
I was experimenting with adding and deleting controls from a screen.
You absolutely CANNOT use this format:
For Each c As Control In Me.Controls
…
MORE
VB.Net - adding and deleting Controls (objects) from Forms
FOR EACH control Add and Delete...
I was experimenting with adding and deleting controls from a screen.
You absolutely CANNOT use this format:
For Each c As Control In Me.Controls
If Strings.Left(c.Name, 3) = "xxx" Then
Me.Controls.Remove(c)
c.Dispose()
End If
Next
This took me a while to work through, but the Controls list is dynamic.
If you delete one, the list is re-formatted to clear out spaces - at least I'm pretty sure that is what happens..
So you have 1,2,3,4,5,6 and you clear the first one. This immediately makes the list = 2,3,4,5,6 - but you have already looked at the first entry
and deleted it, so now you are looking at the second entry, which has a 3 in it. Item 2 is overlooked...
I could not see (initially) why only half of my deletes were happening. Like I say, it took me a while to work it out...
I am now generating a screen that lists all forms, and then all objects on each form. I am then adding them to a group box
in an attempt to let you see the screens quickly, without having to actually call each one.
LESS