Results 1 to 3 of 3

Thread: Solved. The bug was me :-) .Is this a bug? Deleting added controls

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2003
    Posts
    40

    Solved. The bug was me :-) .Is this a bug? Deleting added controls

    Hello,

    I seem to have stumbled on a bug???

    I use code to add textboxes to a form.
    The part to add them works.
    When the form is loaded I can see all of the created texboxes.


    I have added a button to the form that clears all of the textboxes from the form.

    This is the part where it seems to go wrong.
    Not all of the textboxes are removed from the form.


    Thanks for the help,
    Greets,
    J@B@r

    'Code that creates the buttons when the form is loaded.
    Private Sub AddAndDeleteControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim intX As Integer = 16
    Dim intY As Integer = 100
    Dim intTextBoxCounter As Integer = 0
    Dim strStartString As String = "txtStringPart"

    For intTextBoxCounter = 1 To 6
    Dim newTextbox As New TextBox
    newTextbox.Size = New Size(100, 20)
    newTextbox.Location = New Point(intX, intY)
    newTextbox.Name = strStartString & intTextBoxCounter
    'intX += 150
    intY += 20
    Me.Controls.Add(newTextbox)
    Next


    End Sub


    'Code that is behind the button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCleanScreen.Click
    Dim mycontrol As Control


    For Each mycontrol In Me.Controls
    If TypeOf mycontrol Is TextBox Then
    Me.Controls.Remove(mycontrol)
    mycontrol.Dispose()
    'mycontrol.BackColor = System.Drawing.Color.Yellow

    End If
    Next
    End Sub
    Last edited by J@b@r; Oct 12th, 2003 at 03:06 PM.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    No, this isn't a bug, it is actually something you are doing without realizing it. I at first thought it was a bug also....but then I realized something.

    When you are going through a foreach loop and removing items that are used when processing that loop, you get unexpected results. This is because the next item will become the current item when you remove it. So when the loop gets to increment, it is skipping one.

    So to explain it a little further I give you this: (the numbers one through 6 are your text boxes.
    The current order in the form.controls collection:
    1 <-Current item
    2
    3
    4
    5
    6

    First time through the loop, you are looking at number 1. You remove it , now the order looks like this:
    2 <-Current item
    3
    4
    5
    6

    But to the loop, we are still on the first item, which is now 2. So when the loop gets back up to the top, our current item is now 3. So every time we remove something, we are skipping the next item in the collection when processing our loop.
    2
    3 <-Current Item. Notice 2 is still in the collection.
    4
    5
    6

    Hope that helps expain what is happening. To solve this, you need to start at the end of the collection and work towards the beginning. Here is code that will work:
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim mycontrol As Control
    
            For intcount As Integer = Me.Controls.Count - 1 To 0 Step -1
                If TypeOf Me.Controls(intcount) Is TextBox Then
                    Me.Controls.RemoveAt(intcount)
                End If
            Next
        End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2003
    Posts
    40
    Of course,

    Guess I kind of forgot about that.

    I found a sollution by adding the controls to an array of controls.
    Then I run through the elements of the array.
    And delete them, one by one.

    Thanks for the reply.
    Greets J@B@r

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width