Hi everyone,
I was curious....is there a preferred way to save/serialize the settings
of WinForm's controls? I'd like to be able to save the user's values when
they close their form, so they're available the next time they open the
program. I'm sure it's not as easy as saving a WebForm's viewstate, but if
you have any tips or suggestions I'd appreciate it. Thanks!
billr - 06 Aug 2005 01:03 GMT
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);
}
}

Signature
--
Of all words of tongue and pen, the saddest are: "It might have been"
Jim Bancroft - 08 Aug 2005 17:16 GMT
Thank you! Great stuff in your post...
-Jim
billr - 16 Aug 2005 20:45 GMT
Was this post helpful to you?
if so, maybe you should click on the button to say so (others might benefit)

Signature
Of all words of tongue and pen, the saddest are: "It might have been"
Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
> Thank you! Great stuff in your post...
>
> -Jim
Özden Irmak - 08 Aug 2005 14:51 GMT
Hi,
If you are looking to store size, position changes in your form and also
want your form to reflect to screen resolution changes, take a look at Klik!
SizeLib.Net, it might help you :
http://www.kliksoft.com/?S=2&SS=11
Regards,
Özden
> Hi everyone,
>
[quoted text clipped - 4 lines]
> viewstate, but if you have any tips or suggestions I'd appreciate it.
> Thanks!