Solved

VB.NET Show form after controls are rendered

Posted on 2009-05-13
13
1,698 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
ID: 24378472
you can Load your form first before showing it.
0
 

Author Comment

by:WyleOP
ID: 24378510
For example?
0
 
LVL 39

Expert Comment

by:abel
ID: 24378525
For 50p? That must be real easy! Use:

Me.SuspendLayout()

' do your thing of adding controls

Me.ResumeLayout()
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

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

Expert Comment

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

Expert Comment

by:lkalvin
ID: 24401351
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
 

Author Comment

by:WyleOP
ID: 24411951
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
ID: 24413519
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
ID: 24413601
Since I'm not dynamically adding controls, where do you suggest I put the SuspendLayout code?
0
 
LVL 39

Expert Comment

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

Accepted Solution

by:
abel earned 50 total points
ID: 24414528
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
ID: 24415925
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
ID: 24416921
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 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