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. :-)
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.
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.