0

I have a VB.NET winforms application. One of its forms, contains a flowlayoutpanel which I populate dynamically (programmatically) on form load event with pairs of label-textbox. My flowlayoutpanel is created in design time with all the properties set to default except below ones:

  • AutoSize: true
  • AutoScroll: true
  • Dock: fill
  • FlowDirection: TopDown

Then I populate it using below code within the form load event:

Private Sub MyForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
Handles Me.Load
    Dim lbl As Label
    Dim txt As TextBox
    Dim flowLayout As FlowLayoutPanel
    Dim g As Graphics


For i As Integer = 0 To 253
    lbl = New Label
    lbl.Text = i.ToString("000") + ":"
    lbl.Anchor = AnchorStyles.None
    lbl.AutoSize = True

    txt = New TextBox
    txt.Text = "<" + i.ToString.PadLeft(3, " ") + ">"
    txt.MaxLength = 5
    txt.Anchor = AnchorStyles.None
    txt.ReadOnly = True
    g = txt.CreateGraphics
    txt.Width = g.MeasureString(txt.Text, txt.Font).Width + 5
    g.Dispose()

    flowLayout = New FlowLayoutPanel
    flowLayout.FlowDirection = FlowDirection.LeftToRight
    flowLayout.AutoSize = True
    flowLayout.Anchor = AnchorStyles.None
    flowLayout.Margin = New Padding(0)
    flowLayout.Padding = New Padding(0)

    flowLayout.Controls.Add(lbl)
    flowLayout.Controls.Add(txt)

    Me.FlowLayoutPnl.Controls.Add(flowLayout)
Next
End Sub

As I have stated above FlowLayoutPnl is created on design time and components on this are added following TopDown flow direction.

Above code produces this result.

The problem here is that on opening this form it takes so much time (a few seconds) to open because it is doing all the stuff in form load event. It takes 35 seconds to open!. When all the stuff is done then form becomes visible after 35 seconds...

So I would like to know if there is some kind of method to speed up form load. I have read some posts here saying about implement and show some kind of splash screen while it loads indicating user that it is being loaded or even perform an asynchronous load. What about creating a background thread from form load event in order to do this stuff? Anyone could guide me in the right direction? A very little example will be highly appreciated. How this kind of problems are normally resolved?

Willy
  • 9,848
  • 22
  • 141
  • 284
  • SuspendLayout/ResumeLayout may help? – Arvo Mar 07 '16 at 09:23
  • Creating and disposing the Graphics object is probably taking a lot of time. Try removing that part and assign a fixed width to all `txt` elements as a test. Let us know how much faster it runs after that. – LightBulb Mar 07 '16 at 09:32
  • 1
    A form with 762 controls will always be a dog. Painting will be quite abysmal. However, trying to find ~100 billion cpu instructions back in this code is going to be very difficult. Don't create dogs. – Hans Passant Mar 07 '16 at 09:32
  • @Arvo putting SuspendLayout at the beginning of the form load event and ResumeLayout at the end is not working. – Willy Mar 07 '16 at 09:55
  • @HansPassant I agree but in this case I have no other choice... I need to represent all these label-textbox pairs in the form. So in cases like this with a lot of controls, what could I do? – Willy Mar 07 '16 at 09:58
  • 1
    Of course you have a choice. A Paint event handler can do it in a handful of milliseconds. Grid layouts are easily tackled with ListView or DataGridView. You just picked the worst possible choice. – Hans Passant Mar 07 '16 at 10:03
  • @LightBulb using a fixed width for all textboxes and removing creating and disposing graphics object is taking about 25 seconds.... it continues being so much... – Willy Mar 07 '16 at 10:03
  • @HansPassant Could you provide me a little example on how to do this? or any place where an example is provided? – Willy Mar 07 '16 at 10:04
  • @HansPassant Do you mean to put all this code inside paint event instead of load event? – Willy Mar 07 '16 at 10:08
  • @HansPassant I have used paint event handler for flowlayoutpanel. I have put all the code there, but protected with a boolean variable in order to it be executed only the first time when form is opened, because if not this event is called every time flowlayoutpanel needs to be updated. Using a boolean variable I avoid the code to be executed every time. The time decresases drastically from 35 seconds to aproximately 4 seconds (but not milliseconds as you stated). Another problems putting all the code within this event is that sometimes information is not printed well at all when scrolling.Why? – Willy Mar 08 '16 at 09:27

0 Answers0