Results 1 to 8 of 8

Thread: [RESOLVED] Why are my controls not being disposed ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,377

    Resolved [RESOLVED] Why are my controls not being disposed ?

    Hi,

    I'm investigating why some control.Dispose commands are not executed.
    I'm using this code...
    Code:
    For Each ctrol In Me.Controls
        Dim fred As Integer = ctrol.TabIndex
    
        If ctrol.Name.contains("Target") Then
     	ctrol.Dispose
        End If
        If ctrol.Name.contains("Topic") Then
            ctrol.Dispose
        End If
        If ctrol.Name.contains("Alpha") Then
            ctrol.text = ctrol.tag
            ctrol.Hide()
        End If
    Next
    Here is a list of the controls Before I run the code: -
    57 Target13 E
    56 Target12 N
    55 Target11 A
    54 Target10 J
    53 Target9
    52 Target8 Y
    51 Target7 T
    50 Target6 I
    49 Target5 M
    48 Target4 A
    47 Target3 L
    46 Target2 A
    45 Target1 C
    44 Topic5 TITLES
    43 Topic4 PEOPLE
    42 Topic3 WORDS
    41 Topic2 FICTION
    40 Topic1 PLACES
    39 AlphaZ Z
    38 AlphaY
    37 AlphaX X
    36 AlphaW W
    35 AlphaV V
    34 AlphaU
    33 AlphaT
    32 AlphaS S
    31 AlphaR R
    30 AlphaQ Q
    29 AlphaP P
    28 AlphaO
    27 AlphaN
    26 AlphaM
    25 AlphaL
    24 AlphaK K
    23 AlphaJ
    22 AlphaI
    21 AlphaH H
    20 AlphaG G
    19 AlphaF F
    18 AlphaE
    17 AlphaD D
    16 AlphaC
    15 AlphaB B
    14 AlphaA
    0 PictureBox1
    6 GoGo EXIT
    7 NewWord New Word
    13 Park
    The list shows the TabIndex for the control, its Name and its Text. All the relevant controls are a TextBox.


    And these are the controls After I run the code: -
    57 Target13 E
    55 Target11 A
    53 Target9
    51 Target7 T
    49 Target5 M
    47 Target3 L
    45 Target1 C
    43 Topic4 PEOPLE
    41 Topic2 FICTION
    39 AlphaZ Z
    38 AlphaY Y
    37 AlphaX X
    36 AlphaW W
    35 AlphaV V
    34 AlphaU U
    33 AlphaT T
    32 AlphaS S
    31 AlphaR R
    30 AlphaQ Q
    29 AlphaP P
    28 AlphaO O
    27 AlphaN N
    26 AlphaM M
    25 AlphaL L
    24 AlphaK K
    23 AlphaJ J
    22 AlphaI I
    21 AlphaH H
    20 AlphaG G
    19 AlphaF F
    18 AlphaE E
    17 AlphaD D
    16 AlphaC C
    15 AlphaB B
    14 AlphaA A
    0 PictureBox1
    6 GoGo EXIT
    7 NewWord New Word
    13 Park
    Why are all the odd numbered controls from 41 to 57 still there ?


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,928

    Re: Why are my controls not being disposed ?

    Quote Originally Posted by Poppa Mintin View Post
    Hi,

    I'm investigating why some control.Dispose commands are not executed.
    I'm using this code...
    Code:
    For Each ctrol In Me.Controls
        Dim fred As Integer = ctrol.TabIndex
    
        If ctrol.Name.contains("Target") Then
     	ctrol.Dispose
        End If
        If ctrol.Name.contains("Topic") Then
            ctrol.Dispose
        End If
        If ctrol.Name.contains("Alpha") Then
            ctrol.text = ctrol.tag
            ctrol.Hide()
        End If
    Next
    Here is a list of the controls Before I run the code: -
    The list shows the TabIndex for the control, its Name and its Text. All the relevant controls are a TextBox.


    And these are the controls After I run the code: -
    Why are all the odd numbered controls from 41 to 57 still there ?


    Poppa
    Dispose doesn't destroy the object, it simply calls a function intended to clean up the object's unmanaged resources.

  3. #3

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,377

    Re: Why are my controls not being disposed ?

    Thanks PD,

    How do I get rid of controls then ?


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  4. #4
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,928

    Re: Why are my controls not being disposed ?

    Quote Originally Posted by Poppa Mintin View Post
    Thanks PD,

    How do I get rid of controls then ?


    Poppa
    Remove then from the controls collection might do it. Not sure if you would need to remove any event handlers as well or not.

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,167

    Re: Why are my controls not being disposed ?

    The fact that every other control is removed properly makes me believe this is the old "removing items from a list while simultaneously traversing the list in a for each statement" issue.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,074

    Re: Why are my controls not being disposed ?

    Disposing a control does remove it from its parent's Controls collection, which means that you are modifying the collection that you're enumerating, which is never a good thing. If you populate an array with the controls first and then enumerate that, things will work as expected because the array will be unaffected by the disposal:
    vb.net Code:
    1. For Each ctrol In Controls.Cast(Of Control)().ToArray()

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,074

    Re: Why are my controls not being disposed ?

    By the way, your code won't compile with Option Strict On because your 'ctrol' variable is type Object. I know this has come up before but, in this case, you must have Option Strict Off. That's backed up by the fact that all your 'contains' calls have a lower-case "c", indicating that VB doesn't know that that is String.Contains so doesn't change the case.

  8. #8

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,377

    Re: Why are my controls not being disposed ?

    Ha !

    "For Each ctrol In Me.Controls" is executed down from highest index to lowest.

    So...
    Code:
    Dim ctrol As Control
            For i = Me.Controls.Count - 1 To 0 Step -1
                ctrol = Me.Controls(i)
                If ctrol.Name.Contains("Target") Then Me.Controls.Remove(ctrol)
                If ctrol.Name.Contains("Topic") Then Me.Controls.Remove(ctrol)
                If ctrol.Name.Contains("Alpha") Then ctrol.Text = ctrol.Tag : ctrol.Hide()
            Next
    Solves my problem.
    This seems to ignore any missing indices.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

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