1

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.

  • You could try creating all the controls first and then adding them all in one go with a call to `AddRange`. You should have been doing that anyway. Always add large numbers of items in a single batch with `AddRange` if you can. That said, you really shouldn't have that many controls on a form at the same time anyway and you still might see some sluggishness. You ought to find a way to page the data and thus significantly reduce the number of controls needed at a time. – John Sep 28 '22 at 06:32
  • Thanks is there any sample of paging the data of FlowLayoutPanel and saving those items. – Mario Sucic Sep 28 '22 at 06:41
  • Add only as many control as you can see on the screen and reassign/redraw when you scroll. – Jodrell Sep 28 '22 at 06:46
  • There is no inbuilt, just-call-a-method way. You need to determine the logic and write code to implement it. – John Sep 28 '22 at 06:50
  • 1
    https://stackoverflow.com/questions/39808934/fake-scrolling-containers-with-very-many-controls/39810717#39810717 – dr.null Sep 28 '22 at 06:50
  • Building your own grid control with FlowLayoutPanel or TableLayoutPanel is a mistake. Judging from the screenshot, a ListView with View = Details will do just fine. – Hans Passant Sep 28 '22 at 08:43
  • But better design of listview or something like that? – Mario Sucic Sep 28 '22 at 10:37
  • 1
    Use `DataGridView` instead. You can get something close to the screenshot and better by just setting some properties in the designer. `RowTemplate.DefaultCellStyle`, `GridColor`, `ColumnHeadersVisible`..etc. See the docs. Also, you can handle the default painting events to draw the different grid parts the way you want. – dr.null Sep 28 '22 at 11:02
  • Thank you @dr.null, how can i add extra space beetween rows, it is very fast when loading rows and opening files also, very useful bro ;) – Mario Sucic Sep 30 '22 at 22:42
  • You're Welcome. Increase the row's [`DividerHeight`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewrow.dividerheight?view=netframework-4.8&f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(System.Windows.Forms.DataGridViewRow.DividerHeight)%3Bk(TargetFrameworkMoniker-.NETFramework%2CVersion%253Dv4.8)%3Bk(DevLang-csharp)%26rd%3Dtrue) property. – dr.null Sep 30 '22 at 22:50

0 Answers0