Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Layout Persistence for your Forms including all of their subcontrols

0.00/5 (No votes)
27 Jan 2005 1  
This article describes how you can use a tiny control to ensure layout persistence for you form.

Introduction

This article describes how you can let arrange your Windows form its layout automatically by giving its handle to the layout persistence control.

Notice: Base idea comes from the article: Persistence of Window State and Appearance for .NET Applications (Alexander Fedorenko). I took his code that cared basically just about the top level controls to be found in the forms components collection. Now making heavy use of recursion, the control is able to re-arrange all controls in forms even if they are subcontrols of subcontrols...

How to use

Simply refer the assembly and use it in your main form as shown.

using Dau.UI.Forms;

Then, create a single instance of the control and pass your main form's handle and the registry key name where it shall (re-)store its settings!

private FormPersistence formPersistence;
public MainForm()
{ 
  InitializeComponent();
  formPersistence = new FormPersistence(this,"Dau\\Test\\Settings");
  formPersistence.IsSavingOnMove = false;
}

The last line tells the control not to save the settings while the form is being resized. This will increase the drawing performance of your application drastically. You won't mention the difference for a small count of controls being located on your form.

How it works

Since I took the idea from another article, I won't describe its clues here, but I'll briefly explain how I improved it to layout all subcontrols.

protected void SaveControlsRecursively ( Control.ControlCollection p_controls, 
          string p_sParentControlConstantsPath) 
{
  string sCurrentPath;
  foreach(Control control in p_controls)
  {
    //update control name for each cycle call

    sCurrentPath = p_sParentControlConstantsPath +"." + control.Name;
    //write this controls values

    SaveControl(control,sCurrentPath );
    //recursuve call to child controls

    if ( m_Recursive ) 
      SaveControlsRecursively ( control.Controls , sCurrentPath );
  }
}

As you can see in the snippet, I call the same function in itself if recursive is true. All control containers do have the property Controls which, if you call, makes it pretty easy to loop through a control and all of its subcontrols recursively. The abort criteria is kind of hidden here but your code won't loop endlessly because the recursive call happens at the end of the control's loop. And if a control has no more subcontrols then the function will exit.

In the function RestoreControl, the most trickiest thing is done:

if ( control.Dock != DockStyle.None )
    control.Bounds = profile.Read(p_sCurrentName + "." + Constants.Bounds );

The function applies the Bounds values only if the control's DockStyle was set to some dockstyle. This is very important because, you must not handle the layout of controls that are using anchors (self positioning anyway).

That's basically all about it. Don't forget to call the persistence object's dispose method because it keeps its registry key open as long as the object is alive in order to ensure best performance!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here