Hi there!
I have a rich text box and in the formatting toolbar (that sticks to its upper border) I have a ComboBox that displays the standard Font sizes. Just like it is in MS Word or Excel.
The user after selecting text will click a size. How do I achieve this?
I know how to handle this when the selected text is written in a single font. I put the following code in the SelectedIndexChanged event of the combobox.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Font font=new Font(richTextBox1.SelectionFont.FontFamily.ToString(),Convert.ToInt16 (comboBox1.Text),richTextBox1.SelectionFont.Style) ;
richTextBox1.SelectionFont = font;
}
This works fine when the Selected text consists of only one font type.
rtb.SelectionFont.FontFamily
The above property yields a value. However, when the user selects a piece of text that consists of more than two font types the
rtb.SelectionFont
property is returning null. And the catch block is catching an ugly NullReferenceException everytime.
I cannot use a font dialogue box or anything that would force me change the UI.
Any suggestion would help.
Regards,
Sid

Sumair SheikhPosted Jul 23, 2011, 7:10 AM
ITS WORK FINE FOR MY EDITOR .. NO I CAN CHANGE THE SIZE OF SELECTED TEXT CONSIST OF DIFFERENT FAMILIES OR FONTS..
CAN U WRITE THE CODE FOR CHANGE THE THE FONT OF SELECTED TEXT..
IF WE CHANGE THE THE FONTS OF SELECTED TEXT CONSIST OF DIFFERENT SIZES... IT WILL CHANGE THE FONTS BUT ALL THE SIZE OF INDIVIDUAL TEXT REMAIN SAME...
Siddhartha SarkarPosted Oct 9, 2008, 11:00 PM
Thank You Ahsley for the help! :)
Ashley ArgilePosted Oct 8, 2008, 11:55 AM
Here's a link to the latest RTF spec, although I have no idea what version the Rich Text box implementation conforms to. Sorry that I can't point you at something a bit more lightweight!
Regards
Ashley.
Siddhartha SarkarPosted Oct 8, 2008, 1:41 AM
Thanks Asley for the wonderful solution. Even I had come to think that the problem was perhaps difficult to solve without playing with the rtf of the rich text box.
The only small correction will be the regular expression. It should be:
string pattern = @"fs\d+";.
I examined the rtf of the richTextBox and found the rtf to be very messy. Can you please refer to some pointer so that I may educate myself with the rich text format stuff.
Ashley ArgilePosted Oct 6, 2008, 6:11 AM
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionFont != null)
{
Font font = new Font(richTextBox1.SelectionFont.FontFamily.ToString(), Convert.ToInt16(comboBox1.Text), richTextBox1.SelectionFont.Style);
richTextBox1.SelectionFont = font;
}
else
{
int fontSize = (Convert.ToInt16(comboBox1.Text)) * 2; // The RTF font size seems to be double the required font size?!?!?
string newFontSize = "fs" + fontSize.ToString();
string pattern = @"fs[0-9]*";
MatchCollection matches = Regex.Matches(richTextBox1.SelectedRtf, pattern);
string rtf = richTextBox1.SelectedRtf; // If we modify the actual SelectedRtf the selection seems to be lost.
foreach (Match match in matches)
{
rtf = rtf.Replace(match.Value, newFontSize);
}
richTextBox1.SelectedRtf = rtf;
}
}
Very rough but just an idea...
Regards
Ashley