Click here to Skip to main content
15,891,908 members
Articles / Programming Languages / C#

Class Library to Automatically Maintain Persistent Variables, Properties and Data Between Sessions

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
11 Jan 2008CPOL15 min read 44.1K   591   43  
A class library that maintains persistent variables, properties and data between sessions in XML files. It supports default values for new variables and events when the values in the XML file are changed
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

// Part of RJConfig V1.3

namespace RJConfig {
	/// <remarks>
	/// A baseclass for a Form that automatically loads and restores the the forms size, position and state to
	/// and from the configuration file using the CfgVarWindowPos class.
	/// </remarks>
	public class FormPosSave:Form {
		/// <summary>
		/// Name of the section where the variables will be stored in the config file.
		/// Set this to an appropriate Section name in the derived class constructor.
		/// The Section can be shared with other settings for the form.
		/// </summary>
		protected string mSectionName=null;
		/// <summary>
		/// The config variable for the position, size and state of the form.
		/// </summary>
		protected CfgVarWindowPos cfgVarPos;

		/// <summary>
		/// Public parameterless constructor.
		/// Adds events for form loaded and closed.
		/// </summary>
		public FormPosSave()
		{
			Load+=new EventHandler(RJFormPosSave_Load);			
			Closed+=new EventHandler(RJFormPosSave_Closed);
		}

		/// <summary>
		/// The form load event which restores the form position, size and state to the last saved (or default).
		/// The section name must have been set before this point.
		/// </summary>
		/// <param name="Sender">The Form object.</param>
		/// <param name="e">Arguments for this event.</param>
		private void RJFormPosSave_Load(object Sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(mSectionName)) {
				cfgVarPos = new CfgVarWindowPos(Config.AppVarInst, mSectionName, this);
					// This constructor also changes the forms position, size and state.
			}
		}

		/// <summary>
		/// The form close event which saves the current form position, size and state to the config variables.
		/// The variables are also transferd to the file config object but not actually saved to file yet.
		/// </summary>
		/// <param name="Sender"></param>
		/// <param name="e"></param>
		private void RJFormPosSave_Closed(object Sender, EventArgs e)
		{
			if (cfgVarPos != null) {
				cfgVarPos.SetCfg(this);
				Config.AppVarInst.Save(cfgVarPos.xpos.SectionName);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Svep DesignCenter
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions