Set font to an object from a line in a textfile

Hi experts, you'll probably laugh at my newbie question.

I wanna read a textfile and choose the font type written within it an apply it on a textbox. If I have a font dialog and apply the settings for fontDialog.Font sets correctly to textbox.Font. I also save fontDialog.Font to a textfile, and it looks something like this:

             Dim row As New System.IO.StreamWriter("file")
             row.WriteLine(fontDialog.Font)
             textbox.Font = teckensnittDialog.Font

When I start my program I want it to read the line and apply the font on the textbox. I tried alot and now I've got this code:

            Dim row As New System.IO.StreamReader("file")
            textBox.Font = New System.Drawing.Font.FromLogFont(row.ReadLine())

So my questions is, do I need to convert the line which looks like this => [Font: Name=Footlight MT Light, Size=15,75, Units=3, GdiCharSet=0, GdiVerticalFont=False] before I apply it on textBox.Font?

I searched this site, MSDN and also spent a lot of time on google:ing this subject, but I haven't found any solutions. You're my last chance :-)
stromberg10Asked:
Who is Participating?
 
Bob LearnedCommented:
Okay, I missed something.  You will need font family and name.

Dim sFontFamily As String = row.ReadLine()
Dim iFontSize as Integer = row.ReadLine()

Dim fontNew As Font = New Font(sFontFamily, iFontSize)
textBox.Font = fontNew

Bob

0
 
Bob LearnedCommented:
Have you tried:

textBox.Font = New Font(Name, Size)

Bob
0
 
stromberg10Author Commented:
Well, no. How do you mean that I can get my saved line to use that code?

What I need is getting [Font: Name=Footlight MT Light, Size=15,75, Units=3, GdiCharSet=0, GdiVerticalFont=False] converted to Font(Name, Size) maybe!?

The code textbox.Font = teckensnittDialog.Font sets the font correctly from the fontDialog, so that's not a problem.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Bob LearnedCommented:
There are various different types of constructors for the Font class (look in the Help file).  First first take on this, is that you need to parse out the text in the text line, and use this constructor:

 Public Sub New(String, Single, GraphicsUnit)

Dim fontNew As Font = New Font(Name, Size, GraphicsUnit)

textBox.Font = fontNew

Bob
0
 
stromberg10Author Commented:
I still don't get this to work, but thanks for your help so far guys!
Now I'm saving fileDialog.Font.FontFamily to the textfile which results in this line gets saved:
[Font: Name=Times New Roman]

When I try to apply it on the textfield like this I get an error; "Property 'FontFamily' is 'ReadOnly'":
textBox.Font.FontFamily = row.ReadLine()

It's getting really frustrated by now.
0
 
Bob LearnedCommented:
To get this to work properly, you need to create a new font class, and give it the appropriate properties in the constructor, as I had shown.

Dim sFontFamily As String = row.ReadLine()
Dim fontNew As Font = New Font(sFontFamily)

textBox.Font = fontNew

Bob
0
 
stromberg10Author Commented:
If I do like you just showed my I get this error:
"Overload resolution failed because no accessible 'New' accepts this number of arguments."

I increse the points to 150 to get this soltion.
Thanks!
0
 
Bob LearnedCommented:
Can you show me the code you have so far?

Bob
0
 
stromberg10Author Commented:
I made some changes, so now only the font family (Verdana, Times New Roman etc.) saves on a single line and on the line under the font size saves, as you can see here:

Save the settings:

            Dim row As New System.IO.StreamWriter("settings.ini")

            row.WriteLine(textfield.BackColor.ToArgb.ToString)
            row.WriteLine(textfield.BackColor.ToArgb.ToString)
            row.WriteLine(fontDialog.Font.FontFamily.Name)
            row.WriteLine(fontDialog.Font.Size)
            row.Close()

Apply the settings:

            Dim row As New System.IO.StreamReader("settings.ini")

            textBox.BackColor = Color.FromArgb(Integer.Parse(row.ReadLine()))
            textBox.ForeColor = Color.FromArgb(Integer.Parse(row.ReadLine()))

            Dim sFontFamily As String = row.ReadLine()
            Dim fontNew As Font = New Font(sFontFamily)
            textBox.Font = fontNew

            row.Close()

If anything seems terrible wrong, please tell me.
I translating this code from swedish because it's my primarly language, so there might me some small mistakes somewhere.
0
 
stromberg10Author Commented:
Bob, you're an angel!
Big thanks and good luck with everything.
0
 
Bob LearnedCommented:
You're welcome, and good luck to you too.

Bob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.