views:

34

answers:

1

Hi there,

I have a text file with the following info in it, on a single line:

-16777216
Arial
7.8
Bold

(-16777216 is the color of the text in ARGB format).. How would I be able to set this text as the font properties for a label at runtime? I have googled this but couldn't find anything related specifically to my problem. Can anyone please offer any suggestions/help?

thanks lots :) jase

+1  A: 

Assuming all four lines are always there...

var fontSettings = System.IO.File.ReadAllLines("fontsettings.txt");

int color = int.Parse(fontSettings[0], System.Globalization.NumberStyles.Any);
string family = fontSettings[1];
float size = float.Parse(fontSettings[2], System.Globalization.CultureInfo.InvariantCulture);
FontStyle style = (FontStyle)Enum.Parse(typeof(FontStyle), fontSettings[3]);

label1.ForeColor = Color.FromArgb(color);
label1.Font = new Font(family, size, style);
Kawa
hi kawa the following lines have invalid argument errors and i dont know how to fix them: color = int.Parse(fontSettings[0], System.Globalization.NumberStyles.Any); font = fontSettings[1]; float se = float.Parse(fontSettings[2], System.Globalization.CultureInfo.InvariantCulture); FontStyle sle = (FontStyle)Enum.Parse(typeof(FontStyle), fontSettings[3]);
baeltazor
oops MY BAD few spelling mistakes LOL thank s heaps for your answer +1
baeltazor
think i need more coffee this morning
baeltazor
That kind of thing happens. I for one had failed to consider , vs . differences in the float conversion, causing huge text!
Kawa
haha! o well thanks again :D
baeltazor
hey kawa, i keep running into this error saying "Index was outside the bounds of the array." on the following line of code, can u please help me figure out whats going on?:int c = int.Parse(fontSettings[0], System.Globalization.NumberStyles.Any);
baeltazor
This is why I made it several lines. Add a breakpoint and watch the values closely for errors. I used your settings (-16777216 Arial 7.8 Bold) as you typed them, each on a different line. fontSettings should contain each of these as a seperate string.
Kawa
should it matter if there is other information on other lines in the same text file?
baeltazor
As long as the first four are the font settings, the fifth, sixth or whatever-th are irrelevant. This code, as I gave you, stops caring after the fourth line (which is fontSettings[3]).
Kawa