Click here to Skip to main content
6,595,444 members and growing! (19,994 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate

Save and restore Form position and layout with this component

By Igor Gribanov

A component that allows to save and restore layout of any Form without coding.
C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
Posted:25 Sep 2003
Updated:25 Feb 2004
Views:82,285
Bookmarked:72 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
44 votes for this article.
Popularity: 7.09 Rating: 4.31 out of 5
4 votes, 9.1%
1

2
2 votes, 4.5%
3
11 votes, 25.0%
4
27 votes, 61.4%
5

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.

[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().

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.

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.

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

About the Author

Igor Gribanov


Member
I am highly qualified and have experience in programming applications for Windows and web, database development, legacy software integration, technical writing and consulting. Since the beta release I have been working with .NET technologies: C#, VB.NET, MSIL. My other skills include XML/XSLT, Crystal Reports and SQL Server 2000.
dotnet@bk.ru
Occupation: Web Developer
Location: Russian Federation Russian Federation

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralSmall change for app with multiple forms Pinmemberhatton0:31 18 May '07  
GeneralRealPosition, DataGridView Extensions PinmemberMartinus Keller4:40 14 May '07  
GeneralHelp needed Pinmemberpankaj0dm4:20 7 May '07  
GeneralSave/restore datagrid settings PinmemberAndrusM9:29 27 Nov '06  
General.NET 2.0 version PinmemberJohnZonie6:29 25 Nov '06  
GeneralWorks fine in vs 2005 Pinmemberszuwar197910:23 8 May '06  
GeneralCongratulations Pinmembermig1619:10 30 Apr '06  
GeneralHELP Pinmembermattp12148413:52 6 Dec '04  
Generalhello Igor PinmemberHera Malik21:25 24 Sep '04  
GeneralHas anbody converted to vb.net? Pinmembersasijrao6:35 8 Sep '04  
GeneralWhat about when the screen resolution changes PinmemberRodgerB10:18 12 Jul '04  
GeneralShowdialog error PinsussShyless0:43 13 Apr '04  
GeneralComplex controls issue... PinsussAnonymous7:57 26 Feb '04  
GeneralModify for UserControl composite controls PinsussPaul J8:43 5 Feb '04  
GeneralRe: Modify for UserControl composite controls PinmemberIgor Gribanov0:50 15 Feb '04  
GeneralMaximized form restore size/position not saved PinmemberQDefender7:15 2 Oct '03  
GeneralRe: Thank you PinmemberIgor Gribanov11:14 2 Oct '03  
GeneralUsing a file instead of registry? PinsussEto21:10 25 Sep '03  
GeneralRe: Using a file instead of registry? PinmemberIgor Gribanov21:37 25 Sep '03  
GeneralRe: Using a file instead of registry? PinmemberAttila Hajdrik22:10 25 Sep '03  
GeneralRe: Using a file instead of registry? PinsitebuilderUwe Keim22:09 25 Sep '03  
GeneralRe: Using a file instead of registry? PinmemberIgor Gribanov3:27 26 Sep '03  
GeneralRe: Using a file instead of registry? PinsitebuilderUwe Keim4:01 26 Sep '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Feb 2004
Editor: Smitha Vijayan
Copyright 2003 by Igor Gribanov
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project