【问题标题】:Font Styles from Variables来自变量的字体样式
【发布时间】:2018-09-09 19:34:04
【问题描述】:

早安,

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

string font_style = "Bold, Italic, Underline, Strikeout";

我要做的是根据设置更改richtextbox的字体样式。如果有多种字体样式,如粗体、下划线和斜体,richtextbox 字体样式需要更改为该样式。从下面的代码中,它只会将字体样式更改为数组的最后一个“Strikeout”,但不会将其更改为粗体、斜体和下划线。请问有什么办法可以解决吗?

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

【问题讨论】:

    标签: c# string winforms enums


    【解决方案1】:

    你应该像这样使用or

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

    【讨论】:

    • 谢谢 Samvel。正是医生吩咐的。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2016-10-23
    • 2013-05-11
    • 2014-03-11
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2017-07-20
    相关资源
    最近更新 更多