Click here to Skip to main content
15,506,163 members
Articles / Programming Languages / Visual Basic
Article
Posted 13 Aug 2004

Stats

54.7K views
1.4K downloads
36 bookmarked

Persist Window State Component

Rate me:
Please Sign up or sign in to vote.
4.48/5 (32 votes)
10 Apr 20053 min read
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. :-)

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.

C#
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


Written By
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.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Dutchboy225-Aug-10 8:07
Dutchboy225-Aug-10 8:07 
GeneralProblem with dual monitors Pin
Enrico Detoma15-Apr-07 6:35
Enrico Detoma15-Apr-07 6:35 
Hi,

I guess there may be a problem with dual monitors as described here http://forum.codecall.net/managed-c/262-dual-monitors-window-position.html[^]:
If you save the location of your program and then load it once the user restarts you need to have code to keep it inside the visible window if they remove the dual monitor. Without this code the user will load your program and never be able to see it.

So here is how you should patch the method LoadInfoFromRegistry() and, in the same way, the method LoadInfoFromXMLFile() (bold italic code below was added):

<br />
		private void LoadInfoFromRegistry()<br />
		{<br />
			// attempt to read state from registry<br />
			Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(this.mRegistryPath);<br />
			string KeyPrefix = this.mParentForm.GetType().Namespace+"."+this.mParentForm.GetType().Name;<br />
<br />
			if(key != null)<br />
			{<br />
				int left = (int)key.GetValue(KeyPrefix+"_Left", mParentForm.Left);<br />
				int top = (int)key.GetValue(KeyPrefix+"_Top", mParentForm.Top);<br />
				int width = (int)key.GetValue(KeyPrefix+"_Width", mParentForm.Width);<br />
				int height = (int)key.GetValue(KeyPrefix+"_Height", mParentForm.Height);<br />
				FormWindowState windowState = (FormWindowState)key.GetValue(KeyPrefix+"_WindowState", <br />
					(int)mParentForm.WindowState);<br />
<br />
				mParentForm.Location = new System.Drawing.Point(left, top);<br />
				mParentForm.Size = new System.Drawing.Size(width, height);<br />
				mParentForm.WindowState = windowState;<br />
<br />
				Screen currentScreen = Screen.FromControl(this.mParentForm);<br />
			<br />
				// Ensure top visible<br />
				if((this.mParentForm.Top < currentScreen.Bounds.Top) || ((this.mParentForm.Top + this.mParentForm.Height) > (currentScreen.Bounds.Top + currentScreen.Bounds.Height)))	<br />
				{<br />
					this.mParentForm.Top = currentScreen.Bounds.Top;<br />
				}<br />
<br />
				// Ensure at least 60 px of Title Bar visible<br />
				if(((this.mParentForm.Left + this.mParentForm.Width - 60) < currentScreen.Bounds.Left) || ((this.mParentForm.Left + 60) > (currentScreen.Bounds.Left + currentScreen.Bounds.Width))) <br />
				{<br />
					this.mParentForm.Left = currentScreen.Bounds.Left;<br />
				}			<br />
<br />
				this.mWindowInfo.Left = mParentForm.Left;<br />
				this.mWindowInfo.Top = mParentForm.Top;<br />
				this.mWindowInfo.Height = mParentForm.Height;<br />
				this.mWindowInfo.Width = mParentForm.Width;<br />
				this.mWindowInfo.WindowState = mParentForm.WindowState;<br />
			}<br />
<br />

GeneralTranslate to C++/CLI Pin
Tomas7-Oct-06 8:24
Tomas7-Oct-06 8:24 
GeneralRe: Translate to C++/CLI Pin
angus_grant7-Oct-06 13:00
angus_grant7-Oct-06 13:00 
QuestionHow to setting Xml file path Pin
developeradmin20-Sep-06 1:34
developeradmin20-Sep-06 1:34 
AnswerRe: How to setting Xml file path Pin
angus_grant7-Oct-06 12:59
angus_grant7-Oct-06 12:59 
GeneralDoesn't do anything... Pin
godbrain26-Mar-05 22:48
godbrain26-Mar-05 22:48 
GeneralRe: Doesn't do anything... Pin
angus_grant28-Mar-05 12:47
angus_grant28-Mar-05 12:47 
GeneralRe: Doesn't do anything... Pin
David6116-Sep-10 18:27
David6116-Sep-10 18:27 
GeneralRegistryPath Pin
Tiberius DULUMAN10-Nov-04 21:48
Tiberius DULUMAN10-Nov-04 21:48 
QuestionSimple class rather than a Control? Pin
Josh Blair15-Sep-04 12:02
Josh Blair15-Sep-04 12:02 
AnswerRe: Simple class rather than a Control? Pin
angus_grant15-Sep-04 12:29
angus_grant15-Sep-04 12:29 
GeneralRe: Simple class rather than a Control? Pin
Josh Blair15-Sep-04 12:32
Josh Blair15-Sep-04 12:32 
GeneralRe: Simple class rather than a Control? Pin
angus_grant15-Sep-04 20:30
angus_grant15-Sep-04 20:30 

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.