|
-
Aug 12th, 2010, 02:04 PM
#1
Thread Starter
Lively Member
Save/Load Font Settings
I am trying to save the last used font, font color, and background color. Then when starting myapp up again, read the data and use the last used settings. Make since?
Here is what I have so far:
User pick new font &/or color
Code:
If FontDialog1.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
MainText.Font = FontDialog1.Font
MainText.ForeColor = FontDialog1.Color
End If
FontType = FontDialog1.Font.ToString
FontColor = FontDialog1.Color.ToString
Last used font and color saved to file
Code:
FileOpen(1, Application.StartupPath & "\Settings.txt", OpenMode.Output)
WriteLine(1, "Font = " & FontType)
WriteLine(1, "FontColor = " & FontColor)
FileClose(1)
Program retrieves last used setting when opening
Code:
If System.IO.File.Exists(Application.StartupPath & "\Settings.txt") = True Then
FileOpen(1, Application.StartupPath & "\Settings.txt", OpenMode.Input)
Do Until EOF(1)
Input(1, strinput)
If Trim(strinput).StartsWith("Font") Then
split = strinput.Split("=")
FontType= Trim(split(1))
MainText.Font = FontType '***ERROR HERE
End If
Loop
FileClose(1)
End If
*** Value of type 'string' cannot be converted to 'System.Drawing.Font'.
Besides getting the value to the right type, it looks like some more trimming will be necessary as well (unless the [brackets] are supposed to be there).
I hope someone out there is smarter than me.
Anyone who does not wonder, is either omnipotent or a fool.
Amerigoware <<<My Projects
-
Aug 12th, 2010, 05:54 PM
#2
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
-
Aug 12th, 2010, 08:03 PM
#3
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.
-
Aug 13th, 2010, 02:22 AM
#4
Thread Starter
Lively Member
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!
Anyone who does not wonder, is either omnipotent or a fool.
Amerigoware <<<My Projects
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|