Re: Save/Load Font Settings
I would recommend serializing a class that represents a Font, then with that object recreate a Font class. See the following code:
vb.net Code:
Public Class Form1
Public Sub Serialize(ByVal path As String, ByVal f As Font)
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(SerializableFont))
Using stream As New IO.FileStream(path, IO.FileMode.Create)
serializer.Serialize(stream, SerializableFont.FromFont(f))
End Using
serializer = Nothing
End Sub
Public Function Deserialize(ByVal path As String) As Font
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(SerializableFont))
Dim sf As SerializableFont = Nothing
Using stream As New IO.FileStream(path, IO.FileMode.Create)
sf = CType(serializer.Deserialize(stream), SerializableFont)
End Using
serializer = Nothing
If sf IsNot Nothing Then
Return SerializableFont.FromSerializableFont(sf)
End If
Return Nothing
End Function
'//supporting class that represents a Font
<Serializable()> _
Public Class SerializableFont
'//properties
Private _familyName As String
Public Property FamilyName() As String
Get
Return Me._familyName
End Get
Set(ByVal value As String)
Me._familyName = value
End Set
End Property
Private _size As Single
Public Property Size() As Single
Get
Return Me._size
End Get
Set(ByVal value As Single)
Me._size = value
End Set
End Property
Private _style As System.Drawing.FontStyle
Public Property Style() As System.Drawing.FontStyle
Get
Return Me._style
End Get
Set(ByVal value As System.Drawing.FontStyle)
Me._style = value
End Set
End Property
'//methods
Public Shared Function FromFont(ByVal f As Font) As SerializableFont
Dim sf As New SerializableFont()
With sf
.FamilyName = f.FontFamily.Name
.Size = f.Size
.Style = f.Style
End With
Return sf
End Function
Public Shared Function FromSerializableFont(ByVal sf As SerializableFont) As Font
Return New Font(sf.FamilyName, sf.Size, sf.Style)
End Function
End Class
End Class
Re: Save/Load Font Settings
Why not just use My.Settings? It supports both Font and Color. If you bind those properties to the settings then you don't even need any code at all to load and save.
Re: Save/Load Font Settings
Thank you jmcilhinney. I just learned how to do that in another forum and it works great. I wish I had know about that months ago!