Correct removal of control from FlowLayoutPanel

How to set user control in FlowLayoutPanel correctly?

Is it enough flowlayoutpanel1.Controls.RemoveAt(i)?

I just can't find .Dispose () for flowlayoutpanel1.Controls...

+3
source share
4 answers

If you want to delete all controls, you can iterate over the control collection back, rather than create a copy (see below).

I have found that this provides a better solution, especially if you intend to re-fill it. GC forced collection helps control memory where there are a large number of controls.

FlowLayoutPanel.SuspendLayout();

if (FlowLayoutPanel.Controls.Count > 0) {
    for (int i = (FlowLayoutPanel.Controls.Count - 1); i >= 0; i--) {
        Control c = FlowLayoutPanel.Controls[i];
        c.SomeEvent -= SomeEvent_Handler;
        c.Dispose();
    }
    GC.Collect();
}

FlowLayoutPanel.ResumeLayout();
+2
source

FlowLayoutPanel ? , FlowLayoutPanel. Controls. , Dispose control; FlowLayoutPanel Controls.

, FlowLayoutPanel, . Controls , . Controls .

+1

YourFlowLayoutPanel.Controls.Clear(); ,

+1

Dispose() , .

0
source

Source: https://habr.com/ru/post/1787300/


All Articles