简体   繁体   中英

Cannot “add” bold and italic together in C#

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM