-
Remove user control
I have the following codes:
Code:
For Each ctrl As Control In Me.SplitContainer1.Panel1.Controls
If ctrl.GetType.ToString = "Adjustment.UI.ucSubApproval" Then
Me.SplitContainer1.Panel1.Controls.Remove(ctrl)
End If
Next
In my form, there is three user control, it able to remove the first and the third one, but it's not able to remove the second one. I believe it's caused by the for each statement where it will directly go to the third user control and skip the second after remove the first one.
-
Re: Remove user control
you may be able to use an index in reverse:
for i as integer = Me.SplitContainer1.Panel1.Controls.Count - 1 to 0
-
Re: Remove user control
or create a List (of them) as you perform the test then itterate through your List to do the deleting
-
Re: Remove user control
Hi, I tried this:
Code:
For introw = Me.SplitContainer1.Panel1.Controls.Count - 1 To 0
If Me.SplitContainer1.Panel1.Controls(introw).GetType.ToString = "Adjustment.UI.ucSubApproval" Then
Me.SplitContainer1.Panel1.Controls.RemoveAt(introw)
End If
Next
It won't go into the loop and remove the controls. Something wrong?
-
Re: Remove user control
You need to tell it to go backwards else it will think you mean forward and there is nothing to do
Code:
For introw = Me.SplitContainer1.Panel1.Controls.Count - 1 To 0 Step -1
If the Step is ommitted then it assumes Step +1
-
Re: Remove user control
Generally you shouldn't be altering collections you're iterating. Datamiser's suggestion of iterating in reverse will work but note he is using a For...Next loop, a For Each...Next cannot work for this. Mur16's suggestion of using another list is good to go too.