2009-11-05 42 views
1

可以someboody请帮助我将这些字符串/整数转换成可用的字体的东西?它不断想出underneith下面一行红色squigly行:转换成字体信息

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

而且当我试图通过这样设置标签前景色:

mylabel.ForeColor = fcolor; 

的代码,我是:

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; 

非常感谢你

杰森

回答

3

FontSize是一个浮点数并且FontStyle is an enum。因此,它将需要:

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

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

获得一个浮动足够简单...获取字体样式可以有点粘。如果您有代表,比如说,“斜体”或“大胆”的字符串值,你可以用一个愚蠢的,简单的EnumUtils方法如下面来获取枚举值:

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"); 
    } 
} 

如果不是,它会更难。但最终,你需要找到一种方法来转换不管你有into this

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

FontStyle是一个位标志,所以值可以组合,像这样:

FontStyle myStyle = FontStyle.Bold | FontStyle.Italic; 

本方式解析问题贴纸。