Stream memStream=new MemoryStream();
IFormatter formatter=new BinaryFormatter();
formatter.serialize(memStream, object);
Object obj = formatter.deserialize(memStream);
[Serializable]
internal class FormState
{
private readonly FormWindowState _windowState;
private readonly Point _formSize;
private FormState()
{
}
public FormState(FormWindowState state, int width, int height)
{
_windowState = state;
_formSize = new Point(width, height);
}
public FormWindowState WindowState
{
get { return _windowState; }
}
public Point Size
{
get { return _formSize; }
}
}
[Serializable]
class FormStateCollection
{
private IDictionary<string, FormState> _formPersistanceHandlers;
public static FormStateCollection Instance { get; private set; }
static FormStateCollection()
{
Instance =
FileHelper.ReadFromFile<FormStateCollection>
(string.Format(Resources.FilePath, Directory.GetCurrentDirectory())) ??
new FormStateCollection();
}
#region Methods
/// <summary>
/// Adds Persistance Handler To The Dictionary(Thread Safe Way)
/// </summary>
/// <param name="formName"></param>
/// <param name="handler"></param>
[MethodImpl(MethodImplOptions.Synchronized)]
public void Persist(string formName, FormState handler)
{
if(!IsInitialized()) _formPersistanceHandlers = new Dictionary<string, FormState>();
if(ContainsKey(formName))
{
_formPersistanceHandlers[formName] = handler;
}
else
{
_formPersistanceHandlers.Add(formName, handler);
}
}
/// <summary>
/// Gets The Persistance Handler For Particular Form
/// </summary>
/// <param name="formName"></param>
/// <returns></returns>
public FormState ReadState(string formName)
{
if (!IsInitialized() || !ContainsKey(formName)) return null;
return _formPersistanceHandlers[formName];
}
/// <summary>
/// Checks Whether The Dictionary Has Been Initialized or Not
/// </summary>
/// <returns></returns>
private bool IsInitialized()
{
return _formPersistanceHandlers != null;
}
/// <summary>
/// If Dictionary Contains This Key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private bool ContainsKey(string key)
{
return _formPersistanceHandlers.ContainsKey(key);
}
#endregion
}
public class PersistantForm : Form
{
public PersistantForm()
{
this.Load += PersistantFormBaseLoad;
this.FormClosing += PersistantFormBaseFormClosing;
}
/// <summary>
/// Update Our Persistable Forms Type & Serialize It
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void PersistantFormBaseFormClosing(object sender, FormClosingEventArgs e)
{
FormStateCollection.Instance.Persist(this.GetType().FullName, ReadCurrentState());
}
/// <summary>
/// Apply Last Saved State To This Form
/// </summary>
/// <param name="state"></param>
internal virtual void ApplyState(FormState state)
{
this.WindowState = state.WindowState;
this.Size = new Size(state.Size);
/********************************
* You Can Also Read any other information
* Persisted for this Form by calling
* FormState ReadCustomState method like this:-
* state.ReadCustomState<Rectangle>("key");
********************************/
}
/// <summary>
/// Reads The Previous Saved State of this form
/// </summary>
/// <returns></returns>
private FormState ReadLastState()
{
return FormStateCollection.Instance.ReadState(this.GetType().FullName);
}
/// <summary>
/// Gets The Current State Of This Form
/// </summary>
/// <returns></returns>
internal virtual FormState ReadCurrentState()
{
var currState = new FormState(this.WindowState, this.Size.Width, this.Size.Height);
return currState;
/********************************
* You Can Also Persist any other information
* by calling FormState AddCustomState method:-
* currState.AddCustomState("key","value");
********************************/
}
} //End of class
That’s it, now any form that need to persist its state, can simply derive from this form base class and its state will automatically be saved and retrieved as a part of form life cycle.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Commented: