Delete controls in a FlowLayoutPanel

Hi!

I want to delete every Control in a FlowLayoutPanel which have "GT" in their names. I do it like this:

        For Each Control As Control In Me.FlowLayoutPanel1.Controls
            If InStr(Control.Name, "GT") Then
                Control.Dispose()
                Control = Nothing
            End If
        Next

This works great, but because of a reason I can't figure out only half of the controls gets deleted. Which means I will need to run this code 4 times to delete 8 Controls.
8 -> 4 -> 2 -> 1 -> 0

Does anyone know how I  can do this the right way?
LVL 2
OlcayKAsked:
Who is Participating?
 
appariCommented:
use getupperbound method as follows

Console.WriteLine(Array.GetLowerBound(0))      'returns 0
        Console.WriteLine(Array.GetLowerBound(1))  'returns 0
        Console.WriteLine(Array.GetUpperBound(0)) 'returns 5
        Console.WriteLine(Array.GetUpperBound(1)) 'returns 2
0
 
OlcayKAuthor Commented:
I got it working on my own...

Use this if you have a similar problem yourelf:

        Dim i As Integer = Me.FlowLayoutPanel1.Controls.Count - 1
        Do While i > -1
            If InStr(Me.FlowLayoutPanel1.Controls(i).Name, "GT") Then
                Me.FlowLayoutPanel1.Controls.Remove(Me.FlowLayoutPanel1.Controls(i))
            End If

            i = i - 1
        Loop
0
 
appariCommented:
For iCnt As Integer = Me.FlowLayoutPanel1.Controls.Count - 1 To 0 Step -1
            Dim ctrl As Control = Me.FlowLayoutPanel1.Controls(iCnt)

            Console.WriteLine("before delete :" & ctrl.Name)
            If InStr(ctrl.Name, "GT") Then
                Console.WriteLine("deleted :" & ctrl.Name)
                ctrl.Dispose()
                ctrl = Nothing

            End If
        Next
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
appariCommented:

sorry, when i started entering my response your second post was not there
0
 
OlcayKAuthor Commented:
I dont know if this is ok, but I would like to know something else... I willl give the 250 points to this simple question:

How can I determine the X value of an array like this: Array(X, Y)

Dim Array as Array(5, 2)
How can I get the "5"?
0
 
OlcayKAuthor Commented:
Hi appari... Seems like the point will go to you here... I saw your expert points and hope you can help me with a small thing first...

Lets say I have 2 controls in a FlowLayoutPanel and need to add a component between these two. How can I accomplish this?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.