Results 1 to 4 of 4

Thread: Save/Load Font Settings

  1. #1

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Question 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

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. Public Class Form1
    2.  
    3.     Public Sub Serialize(ByVal path As String, ByVal f As Font)
    4.         Dim serializer As New Xml.Serialization.XmlSerializer(GetType(SerializableFont))
    5.         Using stream As New IO.FileStream(path, IO.FileMode.Create)
    6.             serializer.Serialize(stream, SerializableFont.FromFont(f))
    7.         End Using
    8.         serializer = Nothing
    9.     End Sub
    10.  
    11.     Public Function Deserialize(ByVal path As String) As Font
    12.         Dim serializer As New Xml.Serialization.XmlSerializer(GetType(SerializableFont))
    13.         Dim sf As SerializableFont = Nothing
    14.         Using stream As New IO.FileStream(path, IO.FileMode.Create)
    15.             sf = CType(serializer.Deserialize(stream), SerializableFont)
    16.         End Using
    17.         serializer = Nothing
    18.         If sf IsNot Nothing Then
    19.             Return SerializableFont.FromSerializableFont(sf)
    20.         End If
    21.         Return Nothing
    22.     End Function
    23.  
    24.     '//supporting class that represents a Font
    25.     <Serializable()> _
    26.     Public Class SerializableFont
    27.  
    28.         '//properties
    29.         Private _familyName As String
    30.         Public Property FamilyName() As String
    31.             Get
    32.                 Return Me._familyName
    33.             End Get
    34.             Set(ByVal value As String)
    35.                 Me._familyName = value
    36.             End Set
    37.         End Property
    38.  
    39.         Private _size As Single
    40.         Public Property Size() As Single
    41.             Get
    42.                 Return Me._size
    43.             End Get
    44.             Set(ByVal value As Single)
    45.                 Me._size = value
    46.             End Set
    47.         End Property
    48.  
    49.         Private _style As System.Drawing.FontStyle
    50.         Public Property Style() As System.Drawing.FontStyle
    51.             Get
    52.                 Return Me._style
    53.             End Get
    54.             Set(ByVal value As System.Drawing.FontStyle)
    55.                 Me._style = value
    56.             End Set
    57.         End Property
    58.  
    59.         '//methods
    60.         Public Shared Function FromFont(ByVal f As Font) As SerializableFont
    61.  
    62.             Dim sf As New SerializableFont()
    63.  
    64.             With sf
    65.                 .FamilyName = f.FontFamily.Name
    66.                 .Size = f.Size
    67.                 .Style = f.Style
    68.             End With
    69.  
    70.             Return sf
    71.  
    72.         End Function
    73.  
    74.         Public Shared Function FromSerializableFont(ByVal sf As SerializableFont) As Font
    75.             Return New Font(sf.FamilyName, sf.Size, sf.Style)
    76.         End Function
    77.  
    78.     End Class
    79.  
    80. End Class

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    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
  •  



Click Here to Expand Forum to Full Width