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