Why not set the controls' values as dynamic properties and save the settings
to the app.config file ... for example:
/** ************************************** */
1. Create a static object for reading app.config file
/** ************************************** */
using System;
using System.Configuration;
using System.Xml;
using System.Windows.Forms;
sealed public class DynamicSettings
{
private static AppSettingsReader m_SettingsReader = null;
private static string m_ConfigFile = string.Empty;
private static XmlDocument m_XmlConfigurationFile = null;
static DynamicSettings()
{
m_SettingsReader = new AppSettingsReader();
m_ConfigFile = Application.ExecutablePath + ".config";
m_XmlConfigurationFile = new XmlDocument();
m_XmlConfigurationFile.Load(m_ConfigFile);
}
public static string GetString(string name)
{
return(((string)(m_SettingsReader.GetValue(name, typeof(string)))));
}
public static int GetInt(string name)
{
try
{
return(((int)(m_SettingsReader.GetValue(name, typeof(int)))));
}
catch { return(0); }
}
public static double GetDouble(string name)
{
try
{
return(((double)(m_SettingsReader.GetValue(name, typeof(double)))));
}
catch { return(0.0d); }
}
public static float GetFloat(string name)
{
try
{
return(((float)(m_SettingsReader.GetValue(name, typeof(float)))));
}
catch { return(0.0f); }
}
public static bool GetBoolean(string name)
{
try
{
return(((bool)(m_SettingsReader.GetValue(name, typeof(bool)))));
}
catch { return(false); }
}
public static void Set(string name, object value)
{
try
{
XmlNode node =
m_XmlConfigurationFile.SelectSingleNode("//configuration/appSettings/add[@key='" + name + "']");
node.Attributes["value"].Value = value.ToString();
m_XmlConfigurationFile.Save(m_ConfigFile);
}
catch { }
}
}
/** ************************************** */
2. Make use of your dynamic settings : for example
you might make an abstract Form for all of your
applpication forms to sub-class
/** ************************************** */
public abstract class MyForm : System.Windows.Forms.Form
{
public MyForm() : base() { _DynamicSettings(); }
private void _DynamicSettings()
{
int val = 0;
if((val = DynamicSettings.GetInt(Name.ToLower() + ".Top")) > 0)
this.Top = val;
if((val = DynamicSettings.GetInt(Name.ToLower() + ".Left")) > 0)
this.Left = val;
if((val = DynamicSettings.GetInt(Name.ToLower() + ".Width")) > 0)
this.Width = val;
if((val = DynamicSettings.GetInt(Name.ToLower() + ".Height")) > 0)
this.Height = val;
}
}
public class ApplicationForm1 : MyForm { /* class definition */ }
public class ApplicationForm2 : MyForm { /* class definition */ }
/** ************************************** */
3. Make entries in the .config file for your app
/** ************************************** */
<configuration>
<appSettings>
<!-- ApplicationForm1 -->
<add key="applicationform1.Top" value="20" />
<add key="applicationform1.Left" value="10" />
<add key="applicationform1.Width" value="80" />
<!-- ApplicationForm2 -->
<add key="applicationform2.Top" value="20" />
<add key="applicationform2.Left" value="100" />
<appSettings>
<configuration>
/** ************************************** */
4. You will also most likely want to store any changes
too, for example :
/** ************************************** */
public abstract class MyForm : System.Windows.Forms.Form
{
// same implementation as above except for the following
public MyForm() : base()
{
_DynamicSettings();
this.Move += new EventHandler(this.Window_Moved);
}
protected void Window_Moved(object sender, EventArgs e)
{
string name = ((Form)sender).Name.ToLower();
DynamicSettings.Set(name + ".Top", ((Form)sender).Top);
DynamicSettings.Set(name + ".Left", ((Form)sender).Left);
}
}