Hi,
I have a class that extends TreeNode by adding extra fields that I need:
namespace My.Controls
{
public class MyNewTreeNode : System.Windows.Forms.TreeNode
{
public MyNewTreeNode() : base() { }
public MyNewTreeNode(string text) : base() { }
private int severity;
public int Severity
{
get { return severity; }
set { severity = value; }
}
}
}
My click methods for example are sent an argument from which I can find which Node is clicked by doing e.Node. This node is a TreeNode so I tried to cast it to a MyNewTreeNode so that I could get the severity information from it like so:
MyNewTreeNode nodeIn = (MyNewTreeNode)e.Node;
However, this gave me a cast exception at runtime saying that I couldn't cast from a System.Windows.Forms.TreeNode to a MyNewTreeNode. I tried 'as' instead of the cast but that just changes the problem to a Null Reference exception when I try to do anything with the node (obviously because it can't cast and therefore returns null).
This doesn't happen throughout my code, just [it seems] in certain places (which are no different from other places).
I don't understand why this is happening because my class extends TreeNode.
Anyone have any ideas please :)
Thanks in advance for any help you can off me. Tom

Casting a TreeNode
Looooooka
private void treeView_MouseClick(object sender, MouseEventArgs e)
{
if (((e.Button & MouseButtons.Left) == MouseButtons.Left) && (e.Clicks == 2))
{
AutoMapTreeNode node = HitsNode(e.X, e.Y, this.Nodes[0]) as AutoMapTreeNode;
if (node != null)
DrillDown(node);
}
base.OnMouseClick(e);
}
I assume the following things!
The right method to get the write casted version of AutoMapTreeNode is to modify HitsNode() method to reutrn a AutoMapTreeNode. And solution of this can only be suggested after seing code of HitsNode!!! Or may be you can try it at your own so what to do.
Best Regards,
Readan
My click methods for example are sent an argument from which I can find which Node is clicked by doing e.Node. This node is a TreeNode so I tried to cast it to a MyNewTreeNode so that I could get the severity information from it like so:
MyNewTreeNode nodeIn = (MyNewTreeNode)e.Node;
First Mind this thing, your coustom tree node is a tree node but tree node is not your custom tree node. I hope you understand What I mean. I mean to say you can cast Your Custom Tree Node's object to a Tree Node object but not the inverse.
e.Node probably contains an object of TreeNode so what you need to do is to change e.Node's value by Inheritying from the Respective Event args TreeNode event args or what when click is invoked.
Now you better know how to change TreeNode to CustomTreeNode in Arguments to that event Handler.
Cheers ;-)
Vince Bossio
private void treeView_MouseClick(object sender, MouseEventArgs e)
{
if (((e.Button & MouseButtons.Left) == MouseButtons.Left) && (e.Clicks == 2))
{
AutoMapTreeNode node = HitsNode(e.X, e.Y, this.Nodes[0]) as AutoMapTreeNode;
if (node != null)
DrillDown(node);
}
base.OnMouseClick(e);
}
The HitsNode method returns a System.Windows.Forms.TreeNode which then needs to be sent to the DrillDown method but as an AutoMapTreeNode. I could overload the constructor of that method but would prefer a method of just converting the TreeNode to an AutoMapTreeNode if possible as there are other places where I need to cast to an AutoMapTreeNode for other reasons.
Anything else you need to know to help, just ask.
Dawid Weiss
jackiep
Have you considered putting the necessary infomation in a simple class (not derived from TreeNode) and then storing it in the TreeNode's Tag property (This is exactly what the Tag property is for).
(Note, this is for Winform, which appears to be what you are doing. For webforms, you'd have to use the Value property instead to Tag, and serialize the object, as Value is a string)
Darlek
Can you post your code here So I can tell you what to do...
Best Regards,