C# 来自变量的字体样式

C# 来自变量的字体样式,c#,string,winforms,enums,C#,String,Winforms,Enums,你好 我有一个c#应用程序从设置文件中读取字体样式,格式如下 string font_style = "Bold, Italic, Underline, Strikeout"; 我想做的是根据设置更改richtextbox的字体样式。如果有多种字体样式,如粗体、下划线和斜体,则richtextbox字体样式需要更改为粗体、下划线和斜体。从下面的代码中,它只将字体样式更改为数组的最后一个“删除线”,但不会将其更改为粗体、斜体和下划线。请问我能解决这个问题吗 string font_style =

你好

我有一个c#应用程序从设置文件中读取字体样式,格式如下

string font_style = "Bold, Italic, Underline, Strikeout";
我想做的是根据设置更改richtextbox的字体样式。如果有多种字体样式,如粗体、下划线和斜体,则richtextbox字体样式需要更改为粗体、下划线和斜体。从下面的代码中,它只将字体样式更改为数组的最后一个“删除线”,但不会将其更改为粗体、斜体和下划线。请问我能解决这个问题吗

string font_style = "Bold, Italic, Underline, Strikeout";
string[] fontStrings = font_style.Split(',');

for (int i = 0; fontStrings.Length > i; i++)
{
var fntTab = new Font(FontFamily.GenericSansSerif, 18.0F, FontStyle)Enum.Parse(typeof(FontStyle), fontStrings[i], true));

this.richTextBox1.Font = fntTab;
}

例如,您应该使用

 FontStyle res = FontStyle.Regular;
 for (int i = 0; fontStrings.Length > i; i++)
 {
      res = res | (FontStyle)Enum.Parse(typeof(FontStyle), fontStrings[i], true);
 }

 richTextBox1.Font = new Font(FontFamily.GenericSansSerif, 18.0F, res);

谢谢你,桑维尔。正是医生要的。非常感谢