Solved

VB.NET Show form after controls are rendered

Posted on 2009-05-13
13
1,582 Views
Last Modified: 2013-12-26
I have a VB.NET form with a large number of controls within a tablelayoutpanel.  When I show the form, it takes quite a while to load and causes a great deal of flicker.  Is there a way I can create the form, but not show it until all the controls are rendered?  Right now I'm doing a simple:

Dim frm as myForm()
farm.show()
0
Comment
Question by:WyleOP
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 5

Expert Comment

by:Aanvik
Comment Utility
you can Load your form first before showing it.
0
 

Author Comment

by:WyleOP
Comment Utility
For example?
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
For 50p? That must be real easy! Use:

Me.SuspendLayout()

' do your thing of adding controls

Me.ResumeLayout()
0
 

Author Comment

by:WyleOP
Comment Utility
I'm not dynamically adding controls.  They are added via the designer.  
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Then set them all to invisible. Use the above code and make the visible. Then call ResumeLayout.
0
 

Expert Comment

by:lkalvin
Comment Utility
If you'd like a super-easy solution, do this:
1) Add a Timer to your form
2) Call the timer, "FormLoadTimer" and set the Interval to 100
3) Remove ALL code from your FormLoad event - move all the code into a new method called, "FormInitialize"
4) In the (now empty) FormLoad - turn on your timer, "FormLoadTimer.Enabled = True"
5) In the FormLoadTimer_Tick event, put the following code:
Sub FormLoadTimer_Tick(blah blah) Handles me.FormLoadTimer.Tick
      FormLoadTimer.Enabled = False
      FormInitialize()
End Sub

The form will load, setup all controls - resize the HORRIBLY SLOW TableLayoutPanel, get all of your painting done... Then it'll display the whole thing all at once, nice and clean when the Timer ticks...

Hope this helps.
-LK
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:WyleOP
Comment Utility
I'll give that a try, but becasue of the HORRIBLY SLOW TableLayoutPanel, any moving or resizing of the form causes the same issue.  I may need to look for an alternative to the TableLayoutPanel.  Arranging the controls individually is jut not working.  
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
I don't really understand why you haven't given the SuspendLayout a try. there are many ways of doing so, but the main thing is, whenever you change something, resize something or add something, use SuspendLayout. Here's a post that reports going from 20 seconds rendering to 2-3 seconds http://www.tek-tips.com/viewthread.cfm?qid=1437231&page=4. It shouldn't be too hard to change your design slightly to use this technique.
0
 

Author Comment

by:WyleOP
Comment Utility
Since I'm not dynamically adding controls, where do you suggest I put the SuspendLayout code?
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Hold on, I'll give you an example.
0
 
LVL 39

Accepted Solution

by:
abel earned 50 total points
Comment Utility
Ok, so I tried. I had a hard time making the TableLayouPanel heavy enough to make it load slowly, but I managed by putting on a whole lot of different pictures with different scalings. To make it pretty obvious that it wasn't going smooth, I made the TableLayoutPanel resize with the form_resize event. It took quite a lot processing power and it had this shocky way of redrawing. It just "felt heavy" so to speak.

Then I added the SuspendLayout and ResumeLayout around the code. I made the Table originally Visible=False (in the designer) and showed it during load, and resized it to the right size. This helped a lot! All of a sudden everything ran much smoother and I can now easily resize the grid. I don't have timings, but the processor can now easily cope with it.

My suggestion is: try the same, it will save you a *lot* of time and the solution is remarkably simple to implement:

Private Sub Q24406115_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    TableLayoutPanel1.SuspendLayout()

    TableLayoutPanel1.Visible = True

    TableLayoutPanel1.Top = 0

    TableLayoutPanel1.Left = 0

    TableLayoutPanel1.Width = Me.ClientRectangle.Width

    TableLayoutPanel1.Height = Me.ClientRectangle.Height

    TableLayoutPanel1.ResumeLayout()

End Sub
 

Private Sub Q24406115_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

    TableLayoutPanel1.SuspendLayout()

    TableLayoutPanel1.Top = 0

    TableLayoutPanel1.Left = 0

    TableLayoutPanel1.Width = Me.ClientRectangle.Width

    TableLayoutPanel1.Height = Me.ClientRectangle.Height

    TableLayoutPanel1.ResumeLayout()

End Sub

Open in new window

0
 

Expert Comment

by:lkalvin
Comment Utility
Abel's right ya know... his solution will definately make the 'load' go a ton faster but as for resizing your form with that TableLayoutPanel docked for resizing... you'll have to trap the form's resize to prevent the repeated refreshing of the TableLayoutPanel.

In that case, you might try suspending the layout while your form is resizing (Form.ResizeBegin) and then resume your layout when the resize is complete (Form.ResizeComplete)

Unfortunately this will result in a 'ghosting' look that is very unattractive as well.  In my case(s) I just turn the TableLayoutPanel.Visible = False when the resize begins and back to True when the resize is complete.
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
thanks, lkalvin. Funny, I forgot that this 'difficult' question was just 50p, lol. That's probably why it wasn't split. I think with all the solutions around a split of many points would've been nice ;-)

Was a nice exercise to play a bit with that grid, I never use it normally, but the principles of design, rendering and paint events apply just the same ;-)

One final thought: if you really want that kind of professional looking resizing that some browsers or adobe can do, you can mimic that by grabbing an image of the whole area and resizing that during the resize event. Once it finishes, you remove that overlay image and you use the standard method explained above.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now