0
votes

In my application, I have a feature that allows the user to change the bold/italic/underline styles of the text. However, I have noticed that when the user makes the text bold, it reverts the text to the default size and font and THEN makes it bold. Obviously this is undesired as it would then mean that the user would have to change the font and size of the text again, which is undesirable.

Currently, the code for making the text bold within my application's richtextbox is:

richTextBoxPrintCtrl1.SelectionFont = new System.Drawing.Font(richTextBoxPrintCtrl1.Font,
            richTextBoxPrintCtrl1.SelectionFont.Style ^ FontStyle.Bold);

Where am I going wrong? It does make the text bold, but it reverts the text back to the default size and font... However, the colour is unaffected.

1

1 Answers

1
votes

Try using the sample code provided by MSDN:

  System.Drawing.Font currentFont = richTextBoxPrintCtrl1.SelectionFont;
  System.Drawing.FontStyle newFontStyle;

  if (richTextBoxPrintCtrl1.SelectionFont.Bold == true)
  {
     newFontStyle = FontStyle.Regular;
  }
  else
  {
     newFontStyle = FontStyle.Bold;
  }

  richTextBoxPrintCtrl1.SelectionFont = new Font(
     currentFont.FontFamily, 
     currentFont.Size, 
     newFontStyle
  );

Edit

As per @abalter suggestion I put inside this answer the sample code I write in a comment below. That code probably better match what asked in the question.

if (richTextBoxPrintCtrl1.SelectionFont.Bold == true)
{
    newFontStyle = currentFont.Style ^ FontStyle.Regular;
}
else
{
    newFontStyle = currentFont.Style | FontStyle.Bold;
}