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
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();
base.OnCreateControl ();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
SaveSettings();
base.OnClosing(e);
}
}
History
- 10-30-2003 - Edited for completeness
- 10-29-2003 - Initial Release