Use an XOR rather than an OR to remove a single FontStyle
- for example:
private void btnBold_Click(object sender, EventArgs e)
{
var currentStyle = rtf.SelectionFont.Style;
var newStyle =
rtf.SelectionFont.Bold ?
currentStyle ^ FontStyle.Bold :
currentStyle | FontStyle.Bold;
rtf.SelectionFont =
new Font(
rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size,
newStyle);
}
private void btnItalic_Click(object sender, EventArgs e)
{
var currentStyle = rtf.SelectionFont.Style;
var newStyle =
rtf.SelectionFont.Italic ?
currentStyle ^ FontStyle.Italic :
currentStyle | FontStyle.Italic;
rtf.SelectionFont =
new Font(
rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size,
newStyle);
}
With this implementation, removing the bold or italic style will not affect the other style if it is already applied to the selection.
BONUS:
For additional considerations like reselecting the selection after changing its style, an old DevX tip of the day might interest you too.
Also, the common logic in the style-specific handlers I offered begs to be factored out into a helper method that the style-specific handlers can leverage - e.g. private ChangeStyle(FontStyle style)
.