C# 转换成字体信息
有人能帮我把这些字符串/整数转换成可用的字体吗?它在下面一行的下方不断出现一条红色的扭曲线:C# 转换成字体信息,c#,.net,winforms,fonts,C#,.net,Winforms,Fonts,有人能帮我把这些字符串/整数转换成可用的字体吗?它在下面一行的下方不断出现一条红色的扭曲线: mylabel.FONT = new Font(fname, fsize, fstyle); 当我尝试通过以下操作设置标签前景色时: mylabel.ForeColor = fcolor; 我的密码是: int fcolor = Int32.Parse(match.Groups[5].Value); string fname = match.Groups[6].Value;
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;
多谢各位
jasonFontSize是一个浮点数和。因此,需要:
float fsize = float.Parse(...);
new Font(fname, fsize, GetFontStyle(myValue));
获得一个浮动是很容易的。。。获取字体样式可能会有点粘。如果您有一个表示“Italic”或“Bold”的字符串值,则可以使用下面的简单枚举方法来获取枚举值:
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");
}
}
FontStyle是位标志,因此可以像这样组合值:
FontStyle myStyle = FontStyle.Bold | FontStyle.Italic;
这一方面使解析问题成为一个棘手的问题
FontStyle myStyle = FontStyle.Bold | FontStyle.Italic;