Serialize Forms into memory

Posted by serhio on Stack Overflow See other posts from Stack Overflow or by serhio
Published on 2010-04-09T13:04:36Z Indexed on 2010/04/09 13:13 UTC
Read the original article Hit count: 286

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about .NET

Related posts about winforms