Link to home
Start Free TrialLog in
Avatar of OlcayK
OlcayK

asked on

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?
Avatar of OlcayK
OlcayK

ASKER

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
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

sorry, when i started entering my response your second post was not there
Avatar of OlcayK

ASKER

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"?
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of OlcayK

ASKER

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?