Siddhartha Sarkar

Siddhartha Sarkar

  • 1.3k
  • 49
  • 81.1k

Rich TextBox Font Setting Question

Oct 6 2008 12:15 AM

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


Answers (5)