Is this a bug of RichTextBox control??

  • Thread starter Thread starter Yuelin
  • Start date Start date
Y

Yuelin

Hi all

I are stuck on a problem with RichTextBox control. On a simple form - a
label, a richtext control, a button, in the button even method I write:

private void button1_Click(object sender, System.EventArgs e)
{
try
{
string name = this.richTextBox1.SelectionFont.FontFamily.Name;
this.label1.Text = name;
}
catch (Exception ex)
{
Debug.WriteLine (ex.ToString());
}
}

I just simply want to get the font of the text in the rich text box.

I created a short text in Microsoft Word 2000, saved as Rich Text Format,
then I pasted the text into the Rich Text box on the form. When I high-light
the text in the box
and clicked the button, the program throws a exception:
"System.NullReferenceException: Object reference not set to an instance of
an object".

If I create the text in a pain text editor and paste it into the rich text
box, there is no problem.

Is it because Word contains special characters that are not recognised by
the rich text control? But the text created with Word is in RTF format.
I don't know why there is a problem.

Anyone have notice this problem, or know why?

Many thanks


Yuelin
 
string name = this.richTextBox1.SelectionFont.FontFamily.Name;

and clicked the button, the program throws a exception:
"System.NullReferenceException: Object reference not set to an instance of
an object".

Anyone have notice this problem, or know why?

<snip>

Well the first thing to do is find out what exactly is causing a
NullReferenceException - it could be quite a few things in the above,
given the number of levels of indirection. Break the above up into
separate lines, and work out which of the bits ends up being null, thus
causing the exception. My guess is that it's SelectionFont that returns
null, as it says in the documentation:

<quote>
If the current text selection has more than one font specified, this
property is a null reference (Nothing in Visual Basic).
</quote>

So if (possibly invisibly) you'd got more than one font in the
selection, that would definitely explain it, and it wouldn't be a bug
at all.
 
Back
Top