Good Morning. Well as some of you know my daughter returned home and once again she has reverted to her old ways. Her mother and I have both agreed as of her birthday in February (she will be 18) she will be out. We are in full agreement on this because we need to take control of our home back. And that’s the way it has to be.
Today’s topic is how to serialize Microsoft Word or Microsoft Excel objects. Make it a great day!
So now, to use it in the future, all I have to do is import the Module (modConfig) and the Class (clsConfig)
into my Project and I can save the settings of any form by using the Save and Restore procedures in
the Load and Closing events of the Form… like so.
Save("Config.dat", txtTest, chkTest, lvwFTP, lstFTP)’here we can add more controls
Restore("Config.dat", txtTest, chkTest, lvwFTP, lstFTP)’here we can add more controls
*********This is the class that will store the property values and get serialized****************
<Serializable()> Public Class clsConfig
Public arrStr(20)
Public arrBln(20)
Public arrInt(20)
Public arrLst(20) As ArrayList
End Class
*****This is the module that will tie each property to a variable in the Class, then serialize the Class****************
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Module modConfig
Sub Save(ByVal strFile As String, ByVal ParamArray objArr As Object())
Dim Config As New clsConfig
Dim str, bln, int, arr As Integer
Dim arrTmp As ArrayList
Dim objTmp, colTmp As Object
‘ Check for the type of object.
For Each objTmp In objArr
If TypeOf objTmp Is TextBox Then
‘ Set a variable in the Class to equal the desired property.
Config.arrStr(str) = objTmp.Text
str += 1
ElseIf TypeOf objTmp Is CheckBox Then
Config.arrBln(bln) = objTmp.Checked
bln += 1
ElseIf TypeOf objTmp Is ListBox Then
‘ Create Temp Array… add each element of the "Items" collection to it.
arrTmp = New ArrayList
For Each colTmp In objTmp.Items
arrTmp.Add(colTmp)
Next
‘ Set a variable in the Class to equal the Temp Array.
Config.arrLst(arr) = arrTmp
arr += 1
ElseIf TypeOf objTmp Is ListView Then
arrTmp = New ArrayList
For Each colTmp In objTmp.Items
arrTmp.Add(colTmp)
Next
Config.arrLst(arr) = arrTmp
arr += 1
End If
Next
‘ Serialize the Class.
Dim stmSav As Stream = File.Create(Application.StartupPath + "\" + strFile)
Dim serBin As New BinaryFormatter
serBin.Serialize(stmSav, Config)
stmSav.Close()
End Sub
Sub Restore(ByVal strFile As String, ByVal ParamArray objArr As Object())
Dim Config As New clsConfig
Dim str, bln, int, arr As Integer
Dim objTmp, colTmp As Object
‘ Deserialize
If File.Exists(Application.StartupPath + "\" + strFile) Then
Dim stmRst As Stream = File.OpenRead(Application.StartupPath + "\" + strFile)
Dim serBin As New BinaryFormatter
Config = CType(serBin.Deserialize(stmRst), clsConfig)
stmRst.Close()
End If
‘ Basically the oposite of the first sub…
‘ Instead of tying the existing Property to the Class variable, it takes the Property from the deserialized Class
‘ and sets it to the Property.
For Each objTmp In objArr
If TypeOf objTmp Is TextBox Then
objTmp.Text = Config.arrStr(str)
str += 1
ElseIf TypeOf objTmp Is CheckBox Then
objTmp.Checked = Config.arrBln(bln)
bln += 1
ElseIf TypeOf objTmp Is ListBox Then
For Each colTmp In Config.arrLst(arr)
objTmp.Items.Add(colTmp)
Next
arr += 1
ElseIf TypeOf objTmp Is ListView Then
For Each colTmp In Config.arrLst(arr)
objTmp.Items.Add(colTmp)
Next
arr += 1
End If
Next
End Sub
End Module
Technorati Tags: vb.net,.NET Framework,csharp,Serialize,Microsoft,Word,Excel,Objects,Config,Serializable,ArrayList,System,Runtime,Serialization,Formatters,Binary,TypeOf,Text,Create,Array,element,Items,collection,Stream,File,Application,StartupPath,BinaryFormatter,Deserialize,OpenRead,CType