Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to font & Color

Can somebody please help me with a regex (or something else), I'm really struggling to get this done and can't find anything anywhere that helps me to finish it.

I have a program where the user places some controls on a form. and when they click the save button it goes through all controls on the form and saves their details to a text file (which I know how to do)..like so:

Label
"This is text on a label"
20, 97
Tahoma, 7.5, Regular
-778225617

Explanation:

Control Type
Text property of that control
Location of the control
Font properties for that control
ForeColor for that control.

... This text file that gets created when the user saves it may contain information for just a single control, like shown above, or even multiple controls, like so:

Label
"This is text on a label"
20, 97
Tahoma, 7.5, Regular
-778225617
LinkLabel
"This text belongs to the linklabel text property."
Arial, 20, Bold
-119045893

Explanation:

Control
Text Property
Location
Font Properties
ForeColor
Control
Text Property
Location
Font Properties
ForeColor

...etc... I'm finding this to be hard for me, because I'm not an expert, by far. Can somebody please help me? I also need to convert the Font Property line from string into a Font object so it can be assigned to the Font property of the specified control at runtime.

i'd really appreciate any help at all. Thank you so much.

Thanks jay

like image 587
jay_t55 Avatar asked Feb 19 '26 09:02

jay_t55


2 Answers

You would have do do something like this:

using System;
using System.Drawing;

class Example
{
    static void Main()
    {
        String fontName = "Tahoma, Regular, Size";
        String[] fontNameFields = fontName.Split(',');

        Font font = new Font(fontNameFields[0],
            Single.Parse(fontNameFields[2]),
            (FontStyle)Enum.Parse(typeof(FontStyle), fontNameFields[1]));
    }
}
like image 155
Andrew Hare Avatar answered Feb 21 '26 22:02

Andrew Hare


You can read the text from the file and split the string to an array and then use the overloaded constructor for font class to create the new font object.

For a list of font constructors see

Font Constructor

The size parameter is the em-size, in points, of the new font. So for font sizes in other units you have to take care of that.

like image 27
rahul Avatar answered Feb 21 '26 22:02

rahul