Results 1 to 3 of 3

Thread: Understanding ContainerControl.Dispose Method (Boolean) Correctly

  1. #1

    Thread Starter
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,230

    Understanding ContainerControl.Dispose Method (Boolean) Correctly

    Hi Guys n Girls,

    A quick question of understanding if I may. I have created a Parent User Control which is shown in a Flow Layout Panel in the UI. When this Parent User Control is Created is also accepts numerous other Items through its Constructor which then creates numerous other different Child User Controls within that Parent Control.

    Now, when I need to get rid of the Parent User Control I call the Dispose Method on the Parent Control, which is fine, but I just want to make sure that I am understanding the MSDN Documentation correctly when making sure that I Dispose the Child User Controls within that Parent Control.

    When reading this:-

    ContainerControl.Dispose Method (Boolean)

    The documentation seems clear enough, and I am probably just being a bit paranoid having dealt with Incorrect Disposing issues in the past, but am I Right in understanding that if I call ParentUserControl.Dispose(True) then this will automatically Dispose the Child User Controls created within that particular Parent User Control?

    Many thanks for any insights you can provide guys.

    Cheers,

    Ian

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,685

    Re: Understanding ContainerControl.Dispose Method (Boolean) Correctly

    Disposing of a container also disposes of child controls. You can test it for yourself:-
    vbnet Code:
    1. Public Class Form1
    2.  
    3.  
    4.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    5.         FlowLayoutPanel1.Dispose()
    6.     End Sub
    7.     'Required by the Windows Form Designer
    8.     Private components As System.ComponentModel.IContainer
    9.  
    10.  
    11.     'NOTE: The following procedure is required by the Windows Form Designer
    12.     'It can be modified using the Windows Form Designer.  
    13.     'Do not modify it using the code editor.
    14.     <System.Diagnostics.DebuggerStepThrough()> _
    15.     Private Sub InitializeComponent()
    16.         Me.Button1 = New System.Windows.Forms.Button()
    17.         Me.FlowLayoutPanel1 = New System.Windows.Forms.FlowLayoutPanel()
    18.         Me.ButtonEx1 = New WindowsApplication1.ButtonEx()
    19.         Me.FlowLayoutPanel1.SuspendLayout()
    20.         Me.SuspendLayout()
    21.         '
    22.         'Button1
    23.         '
    24.         Me.Button1.Location = New System.Drawing.Point(59, 13)
    25.         Me.Button1.Name = "Button1"
    26.         Me.Button1.Size = New System.Drawing.Size(183, 23)
    27.         Me.Button1.TabIndex = 0
    28.         Me.Button1.Text = "Dispose of container"
    29.         Me.Button1.UseVisualStyleBackColor = True
    30.         '
    31.         'FlowLayoutPanel1
    32.         '
    33.         Me.FlowLayoutPanel1.Controls.Add(Me.ButtonEx1)
    34.         Me.FlowLayoutPanel1.Location = New System.Drawing.Point(72, 115)
    35.         Me.FlowLayoutPanel1.Name = "FlowLayoutPanel1"
    36.         Me.FlowLayoutPanel1.Size = New System.Drawing.Size(200, 100)
    37.         Me.FlowLayoutPanel1.TabIndex = 1
    38.         '
    39.         'ButtonEx1
    40.         '
    41.         Me.ButtonEx1.Location = New System.Drawing.Point(3, 3)
    42.         Me.ButtonEx1.Name = "ButtonEx1"
    43.         Me.ButtonEx1.Size = New System.Drawing.Size(75, 23)
    44.         Me.ButtonEx1.TabIndex = 0
    45.         Me.ButtonEx1.Text = "ButtonEx1"
    46.         Me.ButtonEx1.UseVisualStyleBackColor = True
    47.         '
    48.         'Form1
    49.         '
    50.         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    51.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    52.         Me.ClientSize = New System.Drawing.Size(284, 262)
    53.         Me.Controls.Add(Me.FlowLayoutPanel1)
    54.         Me.Controls.Add(Me.Button1)
    55.         Me.Name = "Form1"
    56.         Me.Text = "Form1"
    57.         Me.FlowLayoutPanel1.ResumeLayout(False)
    58.         Me.ResumeLayout(False)
    59.  
    60.     End Sub
    61.     Friend WithEvents Button1 As System.Windows.Forms.Button
    62.     Friend WithEvents FlowLayoutPanel1 As System.Windows.Forms.FlowLayoutPanel
    63.     Friend WithEvents ButtonEx1 As WindowsApplication1.ButtonEx
    64.  
    65.  
    66. End Class
    67.  
    68. Public Class ButtonEx
    69.     Inherits Button
    70.  
    71.  
    72.     Protected Overrides Sub Dispose(disposing As Boolean)
    73.         Debug.WriteLine("Button Disposed")
    74.         MyBase.Dispose(disposing)
    75.  
    76.     End Sub
    77.  
    78. End Class
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  3. #3

    Thread Starter
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,230

    Re: Understanding ContainerControl.Dispose Method (Boolean) Correctly

    Hi Niya,

    Many thanks for that nice little bit of code to demonstrate things being Disposed within a Container Control. Now why didn’t I think of that to check my own code? You don’t need to answer that!

    Cheers once again and happy coding.

    Kind regards,

    Ian

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