Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Set font to an object from a line in a textfile

Posted on 2004-09-26
11
Medium Priority
?
497 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
ID: 12157017
Have you tried:

textBox.Font = New Font(Name, Size)

Bob
0
 

Author Comment

by:stromberg10
ID: 12157247
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
ID: 12159386
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:stromberg10
ID: 12160169
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
ID: 12160186
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
 

Author Comment

by:stromberg10
ID: 12160393
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
ID: 12160410
Can you show me the code you have so far?

Bob
0
 

Author Comment

by:stromberg10
ID: 12160558
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 600 total points
ID: 12160587
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
ID: 12160701
Bob, you're an angel!
Big thanks and good luck with everything.
0
 
LVL 96

Expert Comment

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

Bob
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

963 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