views:

117

answers:

1

can someboody please help me convert these strings/ints into a usable font thing? it keeps coming up with a red squigly line underneith the following line:

mylabel.FONT = new Font(fname, fsize, fstyle);

and also when i try to set the labels forecolor by doing this:

mylabel.ForeColor = fcolor;

the code i have is:

int fcolor = Int32.Parse(match.Groups[5].Value);
            string fname = match.Groups[6].Value;
            int fsize = Int32.Parse(match.Groups[7].Value);
            string fstyle = match.Groups[8].Value;

thank you very much

jason

+2  A: 

FontSize is a float and FontStyle is an enum. Thus it would need to be:

float fsize = float.Parse(...);

new Font(fname, fsize, GetFontStyle(myValue));

Getting a float is easy enough... getting the font style can be a bit more sticky. If you have a string value which represents, say, "Italic" or "Bold", you can use a stupid-simple EnumUtils method like below to get the enum value:

private FontStyle GetFontStyle(string input)
{
    return EnumUtils.Parse<FontStyle>("myValue");
}

public static class EnumUtils
{
    public static T Parse<T>(string input) where T : struct
    {
        //since we cant do a generic type constraint
        if (!typeof(T).IsEnum)
        {
            throw new ArgumentException("Generic Type 'T' must be an Enum");
        }
        if (!string.IsNullOrEmpty(input))
        {
            if (Enum.GetNames(typeof(T)).Any(
                  e => e.Trim().ToUpperInvariant() == input.Trim().ToUpperInvariant()))
            {
                return (T)Enum.Parse(typeof(T), input, true);
            }
        }
        throw new Exception("Could not parse enum");
    }
}

If not, it will be harder. But ultimately, you do need to find a way to convert whatever you've got into this:

Regular    Normal text.
Bold       Bold text.
Italic     Italic text.
Underline  Underlined text.
Strikeout  Text with a line through the middle.

FontStyle is a bit flag, so values can be combined like so:

FontStyle myStyle = FontStyle.Bold | FontStyle.Italic;

This aspect makes the parsing issue sticker.

Rex M