473,432 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,432 software developers and data experts.

error instantiating Font

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?
Nov 16 '05 #1
6 3122
Tony <yo***@perigee.net> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Tony <yo***@perigee.net> wrote:
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.
Nov 16 '05 #3
Tony <yo***@perigee.net> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Tony <yo***@perigee.net> wrote:
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.Drawin g.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
Nov 16 '05 #5
Tony <yo***@perigee.net> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
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!
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sean | last post by:
Hi there, I am trying to call a C# web service from an aspx page, I have the asmx file, a user control file ascx and the aspx file. I have verified that the web service is returning correct...
0
by: compuglobalhypermeganetz0r | last post by:
I am having trouble getting an adapter to update my access database, it gives the error Syntax error in INSERT INTO statement. for Line 98: Adapter.Update(WineDS, "tblWines") Below is my...
1
by: Joe | last post by:
I have created a web application using C#. I have done several web based apps in C# and have not had this problem before. I think this may be a defect in the VisualSutdio.net2003. I am using a...
4
by: Eugene Anthony | last post by:
One problem with the code bellow is after this code conn.qDupUser p1,rs I added: set rs = nothing to test the error handling capability.
4
by: Abdhul Saleem | last post by:
Hi, I am recieving error ActiveX component can't create object in the following line in the asp page. set ExcelApp = CreateObject("Excel.Application") Previously this code was working fine....
3
by: bb nicole | last post by:
Below is the code i create when i click on the job title hyperlink, it will display the job information where call from database. But the error message is: Parse error: parse error, unexpected $end...
4
by: dancer | last post by:
I get this error if an item in a radiobutton list is not chosen when filling in a form.. I added a RequiredFieldValidator, but I still get the message. Object reference not set to an instance of...
3
by: dancer | last post by:
I am using Framework 1.1.4322. Who can tell me why I'm getting this error? My code follows Compilation Error Description: An error occurred during the compilation of a resource required to...
10
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.