1
votes

Good day,

I have a c# application reading font styles from the settings file in the format as below.

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

I want to do is to change the font style of the richtextbox as per the settings. If there are multiple font styles like Bold, Underline and Italic, the richtextbox font style needs to change to that. From the code below, it only changes the font style to the last of the array which is "Strikeout" but it does not change it to Bold, Italic and Underline. Any way I can resolve this, please?

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

1 Answers

1
votes

You should use or for example like this:

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