FlowLayoutPanel Problem - Adding 1000 Custom Controls with label. VB.NET

140 Views Asked by At

I'm having problem with FlowLayoutPanel, i have custom control created with labels on it, looks like row with size: 250:20. I'm adding it to Flowlayoutpanel programmatically. When i Add 200 or 300 rows on FlowLayoutPanel, it seems ok, but when i'm importing text file with say 1000 lines, so it needs to create 1000 rows (mycontrol) to FlowLayoutPanel, it tooks 30 seconds its slow. How to add it faster and also delete faster.

My FlowLayoutPanel is covered with custom code:

  Public Sub New()
        MyBase.New()
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.DoubleBuffer, True)
        DoubleBuffered = True
        UpdateStyles()
    End Sub

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H2000000
            Return cp
        End Get
    End Property

Screenshot Here: My FlowLayoutPanel and custom Control(Row with Labels)

Adding Items code:

                       For Each item As String In MyList
                            Dim a As New customControl
                            Dim col(1) As String
                            col(0) = item
                            col(1) = ""
                            a.lblTxt.Text = item
                            FlowLayoutPanel.SuspendLayout()
                            FlowLayoutPanell.Controls.Add(a)
                            FlowLayoutPanel.ResumeLayout()
                        Next

Deleting Items Code:

FlowLayoutPanell.Controls.Clear()

I wanted to use ListView, but it has no options like Transparent Color and other stuff to customize, that's why i use FlowLayoutPanel and customControl.

1

There are 1 best solutions below

0
Federico Galvagni On

I know the question it's a bit old but here is an adjusted version of your code:

FlowLayoutPanel.SuspendLayout()

For Each item As String In MyList
   Dim a As New customControl
   Dim col(1) As String
   col(0) = item
   col(1) = ""
   a.lblTxt.Text = item
   FlowLayoutPanel.Controls.Add(a)
Next

FlowLayoutPanel.ResumeLayout()

By doing this, you are minimizing the number of layout calculations since the controls will be drawn again once you inserted all the items.

For deleting items, your current approach of using FlowLayoutPanell.Controls.Clear() is appropriate. This removes all controls from the FlowLayoutPanel at once, and you don't need to worry about calling SuspendLayout and ResumeLayout in this case, if it's slow you could try to create a new empty FLP and substitute the current one with it, I'm not sure but it should reduce the time needed.