Search code examples
c#colorsrichtextboxrtftext-formatting

How do I add a specific property to a font?


I have a string formatting system in place in which users can use special character combinations to color text, add bold, italics, and underlines, all on a text-centered basis.

My problem is, when a user tries to bold AND underline something, for example, the text only displays the latter property. for example,

{bold char combo}{italic char combo}Hello, World

Would show up as:

Hello, World

Rather than:

Hello, World

Here's an example of the code i'm currently using.

// Other formatting codes...
case 'B': // Bold
   box.SelectionFont = new Font(box.SelectionFont, FontStyle.Bold);
   break;
case 'I': // Italic
   box.SelectionFont = new Font(box.SelectionFont, FontStyle.Italic);
   break;
// Other formatting codes...

Solution

  • Found a solution.

    box.SelectionFont = new Font(box.SelectionFont, box.SelectionFont.Style ^ FontStyle.Bold);
    

    Adapted from "A way to overcome RichTextBox's limitations?"