tags:

views:

130

answers:

1

I'm writing an MDI Application in C#. I'd like a way to store the positions and contents of all the open windows, so that a user can customize the way multiple documents are viewed. Is there a simple way to do this, or will I have to roll my own solution?

+1  A: 

I've seen a few form persistence classes around but they didn't do exactly what I needed. I ended up rolling my own, essentially doing the following:

Control mdiClientControl;
foreach (Control control in Controls)
{
   if (control is MdiClient)
   {
      mdiClientControl = control;
      break;
   }
}

foreach (Form mdiChild in MdiChildren)
{
   string theName = mdiChild.Name + "_Window_Layout";
   DoSave(theName, "Top", mdiChildTop);
   .
   .
   .
   DoSave(theName, "WindowState", (int)mdiChild.WindowState);
   DoSave(theName, "Visible", mdiChild.Visible);
   DoSave(theName, "ChildIndex", theMDIClientControl.Controls.GetChildIndex(mdiChild));
}

DoSave() just stores this info off in some XML file in the user's space, but you could store it differently, of course.

When appropriate, such as at startup, I have a ReadSettings() method that essentially reverses the process, interrogating the saved settings, setting the values. There might be a more elegant solution to the problem, but this one has worked really well for me.

Hope that helps.

itsmatt