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;
本方式解析问题贴纸。