You could look into Serialization.
That's exactly what I needed! I just didn't know what it was called :'-(
Thanks for pointing me in the right direction.
i.e build a class that stores your variables. Create an instance at
program startup, persist it through a module, and when your program starts
up, add a handler to the applicaiton's exit event, and when it exists, use
a binary formatter to save the class to disk.. on startup again,
deserialize that file, variables ready to go..
When I deserialize my file, do I have to go through and set each one
individually like so:
Label1.ForeColor = System.Drawing.ColorTranslator.FromOle(mySettings.color)
Label1.Font.Name = mySettings.thisfont.name
Label1.Font.Size = mySettings.thisfont.size
Or can the class elements be associated with stuff in my form somehow?
Matthew
P.S. My current code looks like this:
'form1.vb contains:
Dim mySettings As Settings = New Settings
mySettings.color = System.Drawing.ColorTranslator.ToOle(Label1.ForeColor)
mySettings.thisfont.name = Label1.Font.Name
mySettings.thisfont.size = Label1.Font.Size
mySettings.Save("settings.xml", mySettings)
'class1.vb contains:
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.Serialization
Imports System.IO
<XmlRoot("config")> Public Class Settings
Private _Name As String
Private _Color As String
Private _Font As thisFont = New thisFont
Public Property name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property color() As String
Get
Return _Color
End Get
Set(ByVal Value As String)
_Color = Value
End Set
End Property
<XmlElement("font")> Public Property thisfont() As thisfont
Get
Return _Font
End Get
Set(ByVal Value As thisfont)
_Font = Value
End Set
End Property
Public Shared Sub save(ByVal path As String, ByVal config As Settings)
Dim xs As XmlSerializer = New XmlSerializer(GetType(Settings))
Dim fs As FileStream = New FileStream(path, FileMode.Create)
xs.Serialize(fs, config)
fs.Close()
End Sub
Public Shared Sub load(ByVal path As String, ByVal config As Settings)
Dim myObject As Settings
Dim xs As XmlSerializer = New XmlSerializer(GetType(Settings))
Dim fs As FileStream = New FileStream(path, FileMode.Create)
myObject = CType(xs.Deserialize(fs), Settings)
fs.Close()
End Sub
End Class
<XmlRoot("font")> Public Class thisFont
Private _name As String
Private _size As String
Private _bold As String
Private _italic As String
Private _strikeout As String
Private _underline As String
<XmlElement("name")> Public Property name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
<XmlElement("size")> Public Property size() As Integer
Get
Return _size
End Get
Set(ByVal Value As Integer)
_size = Value
End Set
End Property
End Class