error instantiating Font

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

I'm trying to set a treeview node font style to Bold:

TreeNode newNode = new TreeNode("Nodename");

Font oldFont = TreeView.Nodes[0].NodeFont; // <---Error occurs here

newNode.NodeFont = new Font(oldFont, System.Drawing.FontStyle.Bold);

oldFont.Dispose();

But it doesn't work because TreeView.Nodes[0].NodeFont always throws an
"Object reference not set to an instance of an object." error. In
QuickWatch the NodeFont shows an <undefined error>.

What's the secret handshake to get this to work?
 
Tony said:
I'm trying to set a treeview node font style to Bold:

TreeNode newNode = new TreeNode("Nodename");

Font oldFont = TreeView.Nodes[0].NodeFont; // <---Error occurs here

newNode.NodeFont = new Font(oldFont, System.Drawing.FontStyle.Bold);

oldFont.Dispose();

But it doesn't work because TreeView.Nodes[0].NodeFont always throws an
"Object reference not set to an instance of an object." error. In
QuickWatch the NodeFont shows an <undefined error>.

What's the secret handshake to get this to work?

Well, the NullReferenceException could come from various things:

o Is TreeView null?
o Is TreeView.Nodes null?
o Is TreeView.Nodes[0] null?
 
Jon Skeet said:
Tony said:
I'm trying to set a treeview node font style to Bold:

TreeNode newNode = new TreeNode("Nodename");

Font oldFont = TreeView.Nodes[0].NodeFont; // <---Error occurs here

newNode.NodeFont = new Font(oldFont, System.Drawing.FontStyle.Bold);

oldFont.Dispose();

But it doesn't work because TreeView.Nodes[0].NodeFont always throws an
"Object reference not set to an instance of an object." error. In
QuickWatch the NodeFont shows an <undefined error>.

What's the secret handshake to get this to work?

Well, the NullReferenceException could come from various things:

o Is TreeView null?
o Is TreeView.Nodes null?
o Is TreeView.Nodes[0] null?

Sorry for the delayed reply.

No, none of those are objects are null. That is what's so strange
about it.
The effect is, now I cannot bold, italicize, or in any other manner
inform the font of the text of a treenode.

Frustrating.
 
Tony said:
Well, the NullReferenceException could come from various things:

o Is TreeView null?
o Is TreeView.Nodes null?
o Is TreeView.Nodes[0] null?

Sorry for the delayed reply.

No, none of those are objects are null. That is what's so strange
about it.
The effect is, now I cannot bold, italicize, or in any other manner
inform the font of the text of a treenode.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Jon Skeet said:
Tony said:
Well, the NullReferenceException could come from various things:

o Is TreeView null?
o Is TreeView.Nodes null?
o Is TreeView.Nodes[0] null?

Sorry for the delayed reply.

No, none of those are objects are null. That is what's so strange
about it.
The effect is, now I cannot bold, italicize, or in any other manner
inform the font of the text of a treenode.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Jon,
Here is the main() from a straight new "Windows Application" Project.
I Added a TreeView (treeView1) to the given Form1 class.

I also see my original post did not set this up quite correctly.
....
static void Main()
{
Form1 f1 = new Form1();

TreeNode tnNewChild = new TreeNode("New Child Node Text is Cut Off, BTW");
Font oldFont = tnNewChild.NodeFont;

// runtime error (THIS IS THE ONE I NEED TO WORK):
//tnNewChild.NodeFont = new Font(oldFont,System.Drawing.FontStyle.Bold);

// runtime error:
// tnNewChild.NodeFont =
// new Font(oldFont.FontFamily,oldFont.Size,System.Drawing.FontStyle.Bold);

// works, but not enough space (width) is allocated for the Boldfaced font.
tnNewChild.NodeFont =
new Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);

//works, but no bold.
//tnNewChild.NodeFont = new Font("Verdana",12);

f1.treeView1.Nodes[0].Nodes.Add(tnNewChild);

Application.Run(f1);
}

(end code)

Thanks,
Tony
 
Tony said:
Here is the main() from a straight new "Windows Application" Project.

For future reference, it's much more helpful if you really *would* post
the complete code - it takes a lot longer to open up VS.NET, find a
throwaway solution, add a project, run the test and then delete the
project and all the files within it than to just edit and compile a
single text file.

In this case, the test code can be reduced to:

using System;
using System.Drawing;
using System.Windows.Forms;

class Test
{
static void Main()
{
Form f1 = new Form();

TreeNode tnNewChild = new TreeNode
("New Child Node Text is Cut Off, BTW");
Font oldFont = tnNewChild.NodeFont;

// runtime error (THIS IS THE ONE I NEED TO WORK):
tnNewChild.NodeFont = new Font
(oldFont,System.Drawing.FontStyle.Bold);
}
}

And the reason is simple: oldFont is null. As the docs for
TreeNode.NodeFont say:

<quote>
If a null reference (Nothing in Visual Basic), the Font used is the
Font property value of the TreeView control that this node is attached
to.
</quote>

So it's perfectly reasonable for it to be null - but you can't create a
new font based on nothing.
 
Jon Skeet said:
And the reason is simple: oldFont is null. As the docs for
TreeNode.NodeFont say:

<quote>
If a null reference (Nothing in Visual Basic), the Font used is the
Font property value of the TreeView control that this node is attached
to.
</quote>

So it's perfectly reasonable for it to be null - but you can't create a
new font based on nothing.

Ah. I was mistakenly assuming that a new TreeNode automatically had an
instantiated NodeFont field.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top