Click here to Skip to main content
11,572,401 members (53,370 online)
Click here to Skip to main content

Persist Window State Component

, 10 Apr 2005 45.6K 1.3K 35
Rate this:
Please Sign up or sign in to vote.
A WinForms component to save window position and sizes to registry or XML file or custom saving based on developer choice.

Introduction

First of all, I must say this is based on Joel Matthias' article "Saving and Restoring the Location, Size and Window State of a .NET Form". I decided that my extensions were radical enough not to send the updated code to him to update his article. Joel used a class, whereas I converted it to use a component which can be dragged onto the form like the built-in Timer component. Have a read through Joel's article and then come back to mine. He explains saving window state settings well, and hence has saved me the trouble. Smile | :)

The developer can choose which saving method to use using the pull-down menu on the Properties window.

  • Registry method saves Form settings to HK_CU\CompanyName\AppName\. The CompanyName and AppName are set in the AssemblyInfo code file.
  • XML method saves settings to "WindowStateInfo.xml" in the app directory.
  • The custom method exposes events for the developer to load and save the settings in his own way. My example in the solution saves the settings to individual text files. But other methods could save to database, FTP server, network share, etc. There are no limits.

I have now added public properties for XML file path location and registry path. The registry path is located under the "CurrentUser" path, so if you used "TestApp" as the path, then the form settings would be saved under HKCU\TestApp. The XML file path can be located under the app location, or as in one usage you could the user's Application Data directory to save settings. This is useful in multi-user computers or terminal server usage.

Using the code

Using the component involves adding a component to the "Components" tab of VS.NET. Once the PersistWindowState choice appears, drag this onto the form you want to save the settings for. The default saving choice is the XML file. That is it. Run your app and the Form settings will be saved to the XML file. When you load the app, the XML file is loaded into a DataSet and searched for settings for the current form.

If you are not using VS.NET, then you can use the following code to use the component. Have a look at the enumeration for the valid choices. You need to set the Form property so the PersistWindowState can attach to the Form's OnLoad, OnMove, OnResize, and OnClosing events. The component automatically binds to the Form when placed.

Mowog.PersistWindowComponent WindowState = new Mowog.PersistWindowComponent();
WindowState.PersistMethod = Mowog.PersistWindowComponent.PersistMethods.XMLFile;
WindowState.XMLFilePath = "users\WindowState.xml"
WindowState.Form = this;

Points of Interest

One interesting thing I learnt is how to setup a component to attach to the Form it is placed in. You don't have to manually set a form property to bind the Timer control to the Form, as with my component. Have a look at this page for an explanation on how to do this. You can ignore this first page. The guy has a bit of a waffle about wrecking his bikes.

History

  • 1.0 - Release to The Code Project.
  • 1.1 - Added public properties for XML file location and registry path.

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

Share

About the Author

angus_grant
Web Developer
Australia Australia
Located in sunny Brisbane Australia, was working for three years in Internet company (ASP website and Exchange 5.5 programming mainly), and have moved on to a bankruptcy and insolvency company.

Working on in-house VB6/SQL Server 2000 application, VB.NET/SQL 2000 apps, C#/SQL 2000 app's and company's ASP/SQL 2000 website (blah, ASP is so yucky now I've used ASP.NET).

Personally working on C# / GDI+ RPG game and some other websites soon to be released.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Dutchboy225-Aug-10 7:07
memberDutchboy225-Aug-10 7:07 
GeneralProblem with dual monitors Pin
Enrico Detoma15-Apr-07 5:35
memberEnrico Detoma15-Apr-07 5:35 
GeneralTranslate to C++/CLI Pin
Tomas7-Oct-06 7:24
memberTomas7-Oct-06 7:24 
GeneralRe: Translate to C++/CLI Pin
minja7-Oct-06 12:00
memberminja7-Oct-06 12:00 
QuestionHow to setting Xml file path Pin
developeradmin20-Sep-06 0:34
memberdeveloperadmin20-Sep-06 0:34 
AnswerRe: How to setting Xml file path Pin
minja7-Oct-06 11:59
memberminja7-Oct-06 11:59 
GeneralDoesn't do anything... Pin
godbrain26-Mar-05 21:48
membergodbrain26-Mar-05 21:48 
GeneralRe: Doesn't do anything... Pin
minja28-Mar-05 11:47
memberminja28-Mar-05 11:47 
GeneralRe: Doesn't do anything... Pin
David6116-Sep-10 17:27
memberDavid6116-Sep-10 17:27 
GeneralRegistryPath Pin
Tiberius DULUMAN10-Nov-04 20:48
memberTiberius DULUMAN10-Nov-04 20:48 
QuestionSimple class rather than a Control? Pin
Josh Blair15-Sep-04 11:02
memberJosh Blair15-Sep-04 11:02 
AnswerRe: Simple class rather than a Control? Pin
minja15-Sep-04 11:29
memberminja15-Sep-04 11:29 
GeneralRe: Simple class rather than a Control? Pin
Josh Blair15-Sep-04 11:32
memberJosh Blair15-Sep-04 11:32 
GeneralRe: Simple class rather than a Control? Pin
minja15-Sep-04 19:30
memberminja15-Sep-04 19:30 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 11 Apr 2005
Article Copyright 2004 by angus_grant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid