Serialize Forms into memory
- by serhio
Background:
In a desktop MDI application we have a lot of forms.
Task:
Save the controls contents of closed forms(textbox texts, checkbox checks etc).
Limitations:
A saved form for (DB/Windows) user? For (DB/Windows) group of users? Both variants may be possible.
Question:
a) What is the best way?
b) if I want do not use files, how to serialize the form into a MemoryStream and then recuperate it if the opening form was been once opened and serialized?
StartingPoint:
Implemented a form that implements ISerializable. Deserialize the form on opening, serialize onclosing:
Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
info.AddValue("tbxExportFolder", tbxExportFolder.Text, GetType(String))
info.AddValue("cbxCheckAliasUnicity", cbxCheckAliasUnicity.Checked, GetType(Boolean))
End Sub
Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
Me.New()
Me.tbxExportFolder.Text = info.GetString("tbxExportFolder")
Me.cbxCheckAliasUnicity.Checked = info.GetBoolean("cbxCheckAliasUnicity")
End Sub
Private Sub SerializeMe()
Dim binFormatter As New Formatters.Binary.BinaryFormatter
Dim fileStream As New FileStream(SerializedFilename, FileMode.Create)
Try
binFormatter.Serialize(fileStream, Me)
Catch
Throw
Finally
fileStream.Close()
End Try
End Sub
Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
SerializeMe()
MyBase.OnClosing(e)
End Sub