[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2087
  • Last Modified:

VB.NET Show form after controls are rendered

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
WyleOP
Asked:
WyleOP
  • 6
  • 4
  • 2
  • +1
1 Solution
 
AanvikCommented:
you can Load your form first before showing it.
0
 
WyleOPAuthor Commented:
For example?
0
 
abelCommented:
For 50p? That must be real easy! Use:

Me.SuspendLayout()

' do your thing of adding controls

Me.ResumeLayout()
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
WyleOPAuthor Commented:
I'm not dynamically adding controls.  They are added via the designer.  
0
 
abelCommented:
Then set them all to invisible. Use the above code and make the visible. Then call ResumeLayout.
0
 
lkalvinCommented:
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
 
WyleOPAuthor Commented:
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
 
abelCommented:
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
 
WyleOPAuthor Commented:
Since I'm not dynamically adding controls, where do you suggest I put the SuspendLayout code?
0
 
abelCommented:
Hold on, I'll give you an example.
0
 
abelCommented:
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
 
lkalvinCommented:
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
 
abelCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now