Formatting problem in RichTextBox

faizal_h

Newcomer
Joined
Sep 18, 2008
Messages
1
Hi All,
I am having a trouble in RichTextBox Control in Windows application,I have three buttons bold,underlined and Italics for the RichTextBox.I am selecting a sentence which is partially regular and partially bold . After selecting that sentence , i am trying to convert the selected sentence into italics , the below code is not giving me the expected output . please help me to fix this issue .


Note: 'rtfRichText' is RichTextBox
C#:
private void btnBold_Click(object sender, EventArgs e)
{
FontStyle style = rtfRichText.SelectionFont.Style;
if (rtfRichText.SelectionFont.Bold)
{
style &= ~FontStyle.Bold;
}
else
{
style |= FontStyle.Bold;
}
rtfRichText.SelectionFont = new Font(rtfRichText.SelectionFont, style);

}

private void btnItalic_Click(object sender, EventArgs e)
{
FontStyle style = rtfRichText.SelectionFont.Style;
FontFamily style2 = rtfRichText.SelectionFont.FontFamily;

if (rtfRichText.SelectionFont.Italic)
{
style &= ~FontStyle.Italic;
}
else
{
style |= FontStyle.Italic;
}
rtfRichText.SelectionFont = new Font(rtfRichText.SelectionFont, style);
}

private void btnUnderLine_Click(object sender, EventArgs e)
{
FontStyle style = rtfRichText.SelectionFont.Style;
if (rtfRichText.SelectionFont.Underline)
{
style &= ~FontStyle.Underline;
}
else
{
style |= FontStyle.Underline;
}
rtfRichText.SelectionFont = new Font(rtfRichText.SelectionFont, style);
}

Thanks and Regards,

Faizal Ahmed.H
 
Last edited by a moderator:
The problem is down to the fact the selected text doesn't have a single font - at a guess you might have to loop over every character and set it's font individually...
 
Back
Top