![]() |
Languages »
C# »
Windows Forms
Intermediate
A class that persists form settings automaticallyBy Scott KrugThis class will save the settings of a form automatically. |
C#, VB, Windows, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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 KrugismSamples" to the list of references at the top
of the form code.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.
PersistentForm class in the Add
Reference, Projects tab.Imports KrugismSamples" to the top of the source.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.
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);
}
}
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 28 Oct 2003 Editor: Nishant Sivakumar |
Copyright 2003 by Scott Krug Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |