Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2005

Tip: Looking for answers? Try searching our database.

Reccomended ways of saving WinForms' "viewstate"?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim Bancroft - 05 Aug 2005 19:30 GMT
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!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.