1

I am trying to add so Bold and Italic can be on the same font (inputted by user) and i have made curStyle -= FontStyle.Italic (and Bold) to work but why can't you just += also to add it.

Well just to experiment i put this in:

FontStyle curStyle = rtbMainWrite.SelectionFont.Style;
if (rtbMainWrite.SelectionFont.Italic == false)
{
      curStyle -= FontStyle.Italic;
} else {
      curStyle -= FontStyle.Italic;
}

rtbMainWrite.SelectionFont = new Font(rtbMainWrite.SelectionFont, curStyle);

But this just added ALL the effects together (Underlined, Strikeout, Bold and Italic) but as i pressed the bold button (-= FontStyle.Bold) the bold disappeared but not the other 3, same with Italic (it removed Italic and left Strikeout and Underlined), thought this might help :).

Also the Italic thing looks the same but with Italic instead of Bold obviously.

  • 3
    You are doing the same operation `curStyle -= FontStyle.Italic;` in both if and else block. – Chetan Jun 26 '20 at 13:00
  • yes i know this was just to experiment and the output that i also wrote might help what i though, but the original code as "curStyle = FontStyle.Italic" in the if, and -= in the else. – Brooke Aniston Jun 26 '20 at 13:03
  • FontStyle is a flag based enum. So you need to use `|` to combine more than on font style. https://stackoverflow.com/questions/5350676/make-font-italic-and-bold – Chetan Jun 26 '20 at 13:07
  • https://stackoverflow.com/questions/9777834/remove-fontstyle-bold-from-a-controls-font – Chetan Jun 26 '20 at 13:09
  • Does this answer your question? [Remove FontStyle Bold from a Control's Font](https://stackoverflow.com/questions/9777834/remove-fontstyle-bold-from-a-controls-font) – Dour High Arch Jun 28 '20 at 18:34

1 Answers1

1

Based on https://stackoverflow.com/a/20641576/5386938

private void ToggleItalic_Click(object sender, EventArgs e)
{
    FontStyle curStyle = rtbMainWrite.SelectionFont.Style;
    if (rtbMainWrite.SelectionFont.Italic)
    {
        curStyle &= ~FontStyle.Italic;
    }
    else
    {
        curStyle |= FontStyle.Italic;
    }
    rtbMainWrite.SelectionFont = new Font(rtbMainWrite.SelectionFont, curStyle);
}

private void ToggleBold_Click(object sender, EventArgs e)
{
    FontStyle curStyle = rtbMainWrite.SelectionFont.Style;
    if (rtbMainWrite.SelectionFont.Bold)
    {
        curStyle &= ~FontStyle.Bold;
    }
    else
    {
        curStyle |= FontStyle.Bold;
    }
    rtbMainWrite.SelectionFont = new Font(rtbMainWrite.SelectionFont, curStyle);
}