473,320 Members | 1,979 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,320 software developers and data experts.

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

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
}
Nov 16 '05 #1
2 2368
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

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
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...

Nov 16 '05 #2
Benny Raymond <be***@pocketrocks.com> wrote in
news:uw**************@TK2MSFTNGP10.phx.gbl:
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.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #3

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

Similar topics

7
by: Andrew | last post by:
created a custom class that is derived from TreeNode, let's call it customTreeNode. I'm trying to use the TreeViewEventArgs (for the AfterSelect event) but I cannot cast to my derived TreeNode. ...
4
by: Jeroen Ceuppens | last post by:
Hi, I want to make something that adds a TreeNode to the end of TreeView, that TreeView Looks like + Level 1 + Level 2 + Level 3 - Level last
8
by: kurotsuke | last post by:
Hi, I need to clone a class (called NodeAbstract) that I derived from TreeNode. I need to clone it to support drag and drop on the treeview. I tried to use the MemberWiseClone (in my own...
0
by: Bishop | last post by:
I've figured out how to create a custom TreeNode with custom properties and I can view those properties at runtime, but I'm not sure how to change them after I've added the custom node to the tree....
5
by: Don | last post by:
I've created a small test class to extend the Treenode object and am having mixed success. In the Treeview's 'BeforeExpand' event I've used code from the help topic "Adding Custom Information to...
5
by: imonline | last post by:
Hi, I have developed a webservice with OTA compliance using WSCF 0.6. Now eveything works fine but the soap messages with paymentcard node is giving me following error. (70, 29). ---&gt;...
1
by: djp | last post by:
Hi I add web reference to my C# client app and call web method that returns TreeNode. I tried to cast WebService TreeNode to Windows Forms TreeNode, but without any effect. Any ideas? Thanks...
1
by: =?Utf-8?B?QkpT?= | last post by:
I have written a Generic method that takes an object, a type, T, and returns a System.Nullable (Of T) depending on whether or not the object IsDBNull. It was working fine until I tried to pass a...
0
by: divya1949 | last post by:
Create a windows c# application which will Read a xml file and populate nodes in the treeview. 1 On selection of treenode display the child nodes of that node in listview control 2. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.