Click here to Skip to main content
15,883,917 members
Articles / Programming Languages / C#
Article

Save and restore Form position and layout with this component

Rate me:
Please Sign up or sign in to vote.
4.71/5 (52 votes)
25 Feb 20042 min read 135.9K   3.6K   81   24
A component that allows to save and restore layout of any Form without coding.

Sample Image - Tester.gif

Introduction

Finally there is no more need to write even a single line of code to persist your window's size, location and state. This article presents a component, which automatically saves these properties to the system registry. In addition, this component allows to save the location and size of any control on a form, including splitters and docked controls.

When the RealPosition component is added to a Form, it gives 2 new boolean properties to visible controls. The first property, RestoreLocation, indicates whether the control's position and size should be persisted. If set to true, the control's position and size are saved when the main form closes. The position and size are restored when the .Load event occurs. The second property, RestoreColumnsWidth, applies only to ListView and inherited controls. It allows saving widths of all columns.

Using the code

Add RestoreState.dll to your designer toolbox. Put the RealPosition component into your form. You will see something like this:

Control testing application

For the main form, the RestoreLocation will be set to true by default. Thus, the position and size of the form will be persisted by default. You can set RestoreLocation=true manually for any other controls which you want to persist.

The data is stored in the system registry. Registry key is represented by realPosition1.RegistryPath. You might want to configure this property for your application.

How it works

RealPosition implements IExtenderProvider, which allows it to "provide" new properties to existing controls at design-time.

C#
[ProvideProperty("RestoreLocation", typeof(Control))] 
[ProvideProperty("RestoreColumnsWidth", typeof(ListView))] 
public class RealPosition : Component, IExtenderProvider 
{ 
    // implementation
}

When RestoreLocation is set to true, the development environment generates the following code, which gives realPosition1 references to objects in InitializeComponent().

C#
this.realPosition1.SetRestoreLocation(this.panel11, true);         
................
this.realPosition1.SetRestoreLocation(this.panel12, true);
................
this.realPosition1.SetRestoreLocation(this, true);

RealPosition attaches to .Load and .Closed events in SetRestoreLocation. It performs save and restore operations in the event handler.

C#
f.Closed += new System.ComponentModel.CancelEventHandler(OnClosed);
f.Load += new System.EventHandler(OnLoad);

Known issues

Suppose that a ListView is contained on a Panel and has Left and Right Anchors enabled. In that case, ListView will resize whenever Panel is resized. On startup, RealPosition resizes all linked controls. First, it restores the size of ListView. Then it restores the Panel. Resizing Panel causes ListView to be resized again, thus giving wrong result. Solution: do not enable RestoreLocation in child controls.

Conclusion

This is my first post, so please rate this article and let me know what you think. I hope a few people will find this class useful. Have fun with it...

Updates

15 February 2004

Now the RealPosition component can be placed on any UserControl. Many thanks to Paul J for his suggestion. The following code finds the parent Form of the UserControl and then attaches to its .Closed event.

C#
while(!(parent is Form) && parent != null)
{
    parent = parent.Parent;
} 
m_parentForm = parent as Form;
if(m_parentForm != null)
{
    m_parentForm.Closed += new EventHandler(OnClosed);
}

Another update prevents restoration of the Form's position, if the Form is located outside of the visible area of the screen.

A ComponentDesigner class is now used to retrieve a reference to a container Control. Thanks to Alvaro Mendez for this technique.

The solution file and compiled RestoreState.dll now require VS2003 and .NET Framework 1.1

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSmall change for app with multiple forms Pin
hatton17-May-07 23:31
hatton17-May-07 23:31 
GeneralRealPosition, DataGridView Extensions Pin
Martinus Keller14-May-07 3:40
Martinus Keller14-May-07 3:40 
GeneralHelp needed Pin
pankaj0dm7-May-07 3:20
pankaj0dm7-May-07 3:20 
GeneralSave/restore datagrid settings Pin
AndrusM27-Nov-06 8:29
AndrusM27-Nov-06 8:29 
General.NET 2.0 version Pin
JohnZonie25-Nov-06 5:29
JohnZonie25-Nov-06 5:29 
GeneralWorks fine in vs 2005 Pin
szuwar19798-May-06 9:23
szuwar19798-May-06 9:23 
GeneralCongratulations Pin
mig1630-Apr-06 18:10
mig1630-Apr-06 18:10 
GeneralHELP Pin
mattp1214846-Dec-04 12:52
mattp1214846-Dec-04 12:52 
Hey i was wondering if you could give me some help on a project that i have due Wednesday December 8th.

1.What code is required for within an mdi interface, open a form first off. secondly, when that form is open, we will enter the data in text boxes and then click the save button on the mdi window. i need when its clicked, to save the form data and save it in a file with a designated name. when mdi loads the next time...if that file has been saved once, i don't want it to load again. it then needs to proceed to loading the next form windows. if that form has never been saved, it needs to load that form.

2.I need to figure out how to backup and load backup files.

3.so those above are what im worried most about. also, i need to somehow figure out how to load a flash window within the mdi interface. and i dont' know if you would know how, but is there a way to make like a calendar, where i could click on a day, and it displays all the information in the form window that is saved on that day?

i know this may be a lot, but i would really appreciate the help...thanks very much
Generalhello Igor Pin
Member 120031424-Sep-04 20:25
Member 120031424-Sep-04 20:25 
QuestionHas anbody converted to vb.net? Pin
sasijrao8-Sep-04 5:35
sasijrao8-Sep-04 5:35 
QuestionWhat about when the screen resolution changes Pin
RodgerB12-Jul-04 9:18
RodgerB12-Jul-04 9:18 
GeneralShowdialog error Pin
shyless12-Apr-04 23:43
shyless12-Apr-04 23:43 
GeneralComplex controls issue... Pin
Anonymous26-Feb-04 6:57
Anonymous26-Feb-04 6:57 
GeneralModify for UserControl composite controls Pin
lilGennie5-Feb-04 7:43
lilGennie5-Feb-04 7:43 
GeneralRe: Modify for UserControl composite controls Pin
I G 19814-Feb-04 23:50
I G 19814-Feb-04 23:50 
GeneralMaximized form restore size/position not saved Pin
QDefender2-Oct-03 6:15
QDefender2-Oct-03 6:15 
GeneralRe: Thank you Pin
I G 1982-Oct-03 10:14
I G 1982-Oct-03 10:14 
QuestionUsing a file instead of registry? Pin
Eto25-Sep-03 20:10
Eto25-Sep-03 20:10 
AnswerRe: Using a file instead of registry? Pin
I G 19825-Sep-03 20:37
I G 19825-Sep-03 20:37 
GeneralRe: Using a file instead of registry? Pin
Attila Hajdrik25-Sep-03 21:10
Attila Hajdrik25-Sep-03 21:10 
AnswerRe: Using a file instead of registry? Pin
Uwe Keim25-Sep-03 21:09
sitebuilderUwe Keim25-Sep-03 21:09 
GeneralRe: Using a file instead of registry? Pin
I G 19826-Sep-03 2:27
I G 19826-Sep-03 2:27 
GeneralRe: Using a file instead of registry? Pin
Uwe Keim26-Sep-03 3:01
sitebuilderUwe Keim26-Sep-03 3:01 
GeneralRe: Using a file instead of registry? Pin
nycos6224-Mar-16 1:54
nycos6224-Mar-16 1:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.