Solved

Set font to an object from a line in a textfile

Posted on 2004-09-26
11
490 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 150 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

831 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