Click here to Skip to main content
12,634,435 members (25,094 online)
Click here to Skip to main content
Add your own
alternative version

Stats

36.5K views
1K downloads
19 bookmarked
Posted

A class that persists form settings automatically

, 28 Oct 2003
Rate this:
Please Sign up or sign in to vote.
This class will save the settings of a form automatically.

Introduction

A common need for Windows forms is to remember the last position, size and state of the form. One way to do this is to call a function when your form loads and closes. I decided to do it the Object Oriented way. If your form inherits this class, it will automatically load and save the the form settings; Left, Top, Height, Width, and State; to a .config file.

Using the code

To use this class in a C# project

  • Add the "PersistentForm.cs" class to you project.
  • Add "using KrugismSamples" to the list of references at the top of the form code.
  • Change the:
    public class Form1 : System.Windows.Forms.Form

    to:

    public class Form1 : PersistentForm

That's it! When the form is loaded, the saved values are set to the form. When the form is closed, the settings are saved.

To use this class in a VB.NET project

  • Add a new existing project to your VB solution, and browse to the "PersistentForm.csproj" file.
  • Add a reference to the PersistentForm class in the Add Reference, Projects tab.
  • Add "Imports KrugismSamples" to the top of the source.
  • Change the:
  • Inherits System.Windows.Forms.Form

    to:

    Inherits PersistentForm

That's it! When the form is loaded, the saved values are set to the form. When the form is closed, the settings are saved.

Overview

This class is very straightforward. It simply inherits the Windows.Forms.Form class. It then overrides the OnCreateControl() and OnClosing() events. By overriding the base events, no additional code needs to be added to the form. (The LoadSettings() and SaveSettings() code is not shown here.)

public class PersistentForm : System.Windows.Forms.Form
{
    public PersistentForm()
    {
    }
    protected override void OnCreateControl()
    {
        LoadSettings();     // Load the saved settings from the file
        base.OnCreateControl ();
    }
   
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        SaveSettings();        // Save the settings to the file
        base.OnClosing(e);
    }
}

History

  • 10-30-2003 - Edited for completeness
  • 10-29-2003 - Initial Release

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

Share

About the Author

Scott Krug
Web Developer
United States United States
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralFunctionality Issues Pin
Steve Miller5-Nov-03 12:08
memberSteve Miller5-Nov-03 12:08 
GeneralNot an Article Pin
Heath Stewart30-Oct-03 4:14
editorHeath Stewart30-Oct-03 4:14 
GeneralRe: Not an Article Pin
ScottTheTall30-Oct-03 18:33
memberScottTheTall30-Oct-03 18:33 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 29 Oct 2003
Article Copyright 2003 by Scott Krug
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid