Solved

Set font to an object from a line in a textfile

Posted on 2004-09-26
11
486 Views
Last Modified: 2013-12-03
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 :-)
0
Comment
Question by:stromberg10
  • 6
  • 5
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Have you tried:

textBox.Font = New Font(Name, Size)

Bob
0
 

Author Comment

by:stromberg10
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
 

Author Comment

by:stromberg10
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:stromberg10
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Can you show me the code you have so far?

Bob
0
 

Author Comment

by:stromberg10
Comment Utility
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 150 total points
Comment Utility
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
 

Author Comment

by:stromberg10
Comment Utility
Bob, you're an angel!
Big thanks and good luck with everything.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
You're welcome, and good luck to you too.

Bob
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now