I have a little problem. I have one 1 RichTextBox and 2 Buttons.
I have that 2 buttons for "toggle Bold FStyle" and "toggle Italic FStyle".
I want to toggle FontStyles without affecting other FontStyles. I hope you understand me.
Below code works when combining FontStyles but is not working when seperating/substracting FontStyles.
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font(richTextBox1.Font, (richTextBox1.SelectionFont.Bold == false ? richTextBox1.SelectionFont.Style | FontStyle.Bold : richTextBox1.SelectionFont.Style));
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font(richTextBox1.Font, (richTextBox1.SelectionFont.Italic == false ? richTextBox1.SelectionFont.Style | FontStyle.Italic : richTextBox1.SelectionFont.Style));
}
The easiest way is to use bitwise XOR (^
), which just toggles the value:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font(richTextBox1.Font,
richTextBox1.SelectionFont.Style ^ FontStyle.Bold);
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font(richTextBox1.Font,
richTextBox1.SelectionFont.Style ^ FontStyle.Italic);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With