views:

463

answers:

1

I've been having a hard time trying to get the right answers for my problem. And have spent numerous days searching online and in documentation and have found nothing.

I have a Text File that contains a bunch of text. And on one of those lines inside the file will contain some Font info like this:

Tahoma,12.5,Regular

Note that the font info will not always have the same font name, size or style so I can't just set it manually.

When this file is opened into my app, it will parse the contents (which I have mostly covered), I just need some help converting the above font string into an actual font object and then assigning that font to a control, i.e. a label etc...

Can somebody please help me with this?

+1  A: 

You will want to use the Font class. Assuming you use String.Split() to parse the text into an array you will want to take each part of the array and use it to create a Font object like:

string s = "Tahoma,12.5,Regular";
string[] fi = s.Split(',');
Font font = new Font(fi[0], fi[1],fi[2]);

I don't have a C# compiler on this Mac so it may not be exactly correct.

Example constructor:

public Font(
string familyName,
float emSize,
FontStyle style
)

Here you need to specify the second argument as a float, so cast the string to a float with:

(float)fi[1]

Next you need to lookup a FontStyle based on what fi[2] is:

   if (fi[2] == "Regular") {
      // set font style
   }
BrianLy
thank you brianly - i have 2 errors though i'm not too sure what they mean exactly. the first one says:Argument '2': cannot convert from 'string' to 'float' ..and the second says:Argument '3': cannot convert from 'string' to 'System.Drawing.FontStyle'..
baeltazor
You need to look at the docs I linked to for the Font constructors. My example has too many arguments. Find a constructor that matches the arguments you want to pass in.
BrianLy
Thank you BrianLy, I really appreciate you helping me. I'm still having the same problem but feel that I am getting closer to what I'm trying to acheive. +1
baeltazor