temporarily disable form redraw

ive written a function that moves and resizes controls in my form, to maintain a certain layout (its worked out with percentages, so things can be centred on the form etc)

thing is when the form is resized it looks a bit ugly, as every control movement causes it to redraw itself. what i'd like to do is to pause the redrawing at the beginning of my function, then re-enable it once my function is complete, so that they all redraw at once...

is this possible


Answer this question

temporarily disable form redraw

  • AAjusLT

    well... the functions work but dont have the desired effect!

    basically, the problem is, when the controls are moved around, some of them seem to have a little lag.

    the listboxes and comboboxes move instantly, but buttons and labels lag behind a little, as if tied to the rest of the form by a piece of elastic - its quite strange!

    using suspendlayout and resumelayout did not unfortunately change anything...

  • Peter Peter

    I believe that you are looking for the Form.SuspendLayout and Form.ResumeLayout methods. Examples can be seen in the InitializeComponent method of just about any form that is created using VS.NET. They use these calls when they are setting all of the new control properites when a form is first loaded to reduce flicker.
  • MilleniumHandAndShrimp

    I'm having the same exact problem as you
    did you solve it
    if so please share your solution

    thanks
    Alejandro

  • Charley_Chen_bj

    thats the one - i knew i'd seen it somewhere but couldnt remember what the functions were called

    thx guys ;)

  • Matt M Jacobsen

    SuspendLayout / ResumeLayout are intended for creation of the form (at least I think), or in cases when you delete or add controls at runtime. Moving or Resizing works on the entire form, Items on that form are moved along with it. I haven't tried it but I suppose one could ovrride the Form "Move-handler" set some boolean flag which is caught by an override of the "Paint-handler" to immediate return when set. Which might give the curious effect that one moves around an empty form. So probably best stick to the default behaviour and take the little lag for granted. (I tested this default behaviour on my 1.6 MHz system and it looked fairly good)
  • Nathan Stricker

    SuspendLayout and ResumeLayout prevent the OnLayout function from being called for a particular container until you're ready. They can be called at any time - and require that if you call suspend, you should call resume.

    However, they only block for that container - not child containers - Eg if your controls are in a panel - then calling Suspend/Resume Layout on the form will only prevent the panel from getting resized.

    If you are directly playing with the Bounds, Left, Right, Top, Bottom, Location, Width and Height properties of the child controls (label, combos you describe) this change is going to happen immediately regardless of suspend layout of the panel.  

    Recommendations:
    - Make sure you're suspending the correct container.
    - Batch up your changes to these properties - if you're setting the position and size of the control, use Bounds to set it rather than making separate calls to width, height etc.
    - Do this work in OnLayout/Layout event

    Theres a great article discussing layout:
    http://msdn.microsoft.com/vbasic/using/columns/wonders/default.aspx pull=/library/en-us/dnforms/html/winforms12102002.asp

    and a more advanced one here:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dndotnet/html/custlaywinforms.asp

    Hope this helps.
    Jessica


  • Dima671

    The Control.SuspendLayout/Control.ResumeLayout should help.
  • temporarily disable form redraw