InvalidCastException when casting from TreeNode to my custom Node...what gives?

B

Benny Raymond

More problems with this... When I run this code, the main form returns
an invalid cast exception as it's executing the line "TreeNode n =
(TreeNode) this.Nodes[0];"

Does anyone know what would cause this? I just want to be able to use
my own node class so that I can store extra data...

=========================


using System;
using System.ComponentModel;
using System.Collections;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace System.Windows.Forms
{
#region TView
public class tView : TreeView
{
#region Constructor
public tView()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
}
#endregion

#region Custom Drawing R Us
protected override void OnPaint(PaintEventArgs pe)
{

/*
* since windows handles the drawing of a tree control,
* we had to use userpaint true, thus the following line
* doesn't work. Normally this line would draw everything
* for me, then i could go back and make some changes...
* :(
*/
//base.OnPaint(pe);

// Declare and instantiate a new pen.
Graphics g = pe.Graphics;
TreeNode n = (TreeNode) this.Nodes[0];

// either draw the first node, or draw the first visible node
if (n.IsVisible)
{
DrawText(g, n);
}
else
{
DrawText(g, (TreeNode) n.NextVisibleNode);
}
}
protected void DrawText (Graphics g, TreeNode n)
{
Rectangle r = new Rectangle(n.Bounds.X, n.Bounds.Y, n.Bounds.Width +
5, n.Bounds.Height);
if (n.IsVisible)
{
/*
* we have to check IsVisible so we aren't drawing
* what we don't have to, this is because next visible
* node doesn't really return the next "visible" node
* it just returns the next node that would be visible
* if we were drawing everything
*/
if (n.IsSelected)
{
g.FillRectangle(SystemBrushes.Highlight, r);
g.DrawString(n.Text, this.Font, SystemBrushes.HighlightText, r);
DrawLines(g,n);
}
else
{
g.DrawString(n.Text, this.Font, SystemBrushes.WindowText, r);
}
}
if (n.NextVisibleNode != null)
{
DrawText(g, ((TreeNode) n.NextVisibleNode));
}
}

protected void DrawLines (Graphics g, TreeNode n)
{
int indent = n.TreeView.Indent;
Rectangle boxBounds = new Rectangle(n.Bounds.X - indent, n.Bounds.Y,
indent, n.Bounds.Height);
g.DrawRectangle(SystemPens.ControlDark, boxBounds);
g.DrawLine(SystemPens.WindowText, boxBounds.Left + 4,
boxBounds.Top + 2, boxBounds.Left + 4, boxBounds.Top + 6);
g.DrawLine(SystemPens.WindowText, boxBounds.Left + 2, boxBounds.Top +
4, boxBounds.Left + 6, boxBounds.Top + 4);

}

#endregion

#region TreeNode
public class TreeNode : System.Windows.Forms.TreeNode
{
private Hashtable _items;

public Hashtable Items
{
get
{
return _items;
}
set
{
_items = value;
}
}

}
#endregion
}
#endregion
}
 
S

ShaneB

If your goal is to store extra data with each node, why not just use the
Node.Tag property to store your extra data? That's what it's for.

public class MyTreeNodeDataClass
{
public Hashtable Items; // get/set removed for clarity
public MyTreeNodeDataClass()
{
}
}
....
MyTreeNodeDataClass m1 = new MyTreeNodeDataClass();
treeView1.Nodes[0].Tag = m1;
....
MyTreeNodeDataClass m2 = (treeView1.Nodes[0].Tag as MyTreeNodeDataClass);
if (m2 != null)
{
// do whatever you want with m2 here...
}

ShaneB
 
C

Chris R. Timmons

More problems with this... When I run this code, the main form
returns an invalid cast exception as it's executing the line
"TreeNode n = (TreeNode) this.Nodes[0];"

Does anyone know what would cause this? I just want to be able
to use my own node class so that I can store extra data...

Benny,

I think the cause of the error is a naming ambiguity. You've named
your class TreeNode. If you don't always use a fully qualified name
for both TreeNode classes (yours and System.Windows.Forms), that may
be causing some confusion on your part. Try changing the name of
your TreeNode class to something else, and see what happens.
 

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

Top