Using Visual Studio to create a Windows Forms application, using C#. I have a RichTextBox. This box supports formatting like colours, bold, and underline.
I've added buttons such that people can format their text right in the box. For example, the Bold button does this:
if (!message.SelectionFont.Bold)
message.SelectionFont = new Font(message.Font, FontStyle.Bold);
else
message.SelectionFont = new Font(message.Font, FontStyle.Regular);
This works fine for bolding and unbolding regular text. The issue is when it is combined with other formatting. The creation of a new Font wipes away the original formatting, and leaves me with pure bold or pure regular text.
How can I preserve original formatting, while changing text to bold?
It shouldn't be hard-coded. I shouldn't be checking if(bold), if(italics), if(underline) for every option, as ideally I'll be adding more options later (colour, font, font size, etc).
Doesn't seem to be an easy way to do this.
This link might be of interest: How do I maintain RichText formatting (bold/italic/etc) when changing any one element?