473,320 Members | 2,133 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.

Casting to a derived TreeNode object (from a TreeViewEventArgs Node)

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. Here is
a snip...

---code snip---
private void _TreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
customTreeNode foo = (customTreeNode)e.Node;
displayInfo(foo);
}

private void displayInfo(customTreeNode cTreeNode) {
//code
}
---code snip---

Following the example from MSDN,
(http://msdn.microsoft.com/library/de...-us/vbcon/html
/vbtsksubclassinglistitemortreenode.asp),
the MSDN example (at the bottom of the page) is almost exactly like
the above code.

At runtime, I get a InvalidCastException. What gives?

In the AfterSelect event handler method, if I do
'e.Node.GetType()'--it is, in fact, a customTreeNode. If I do not
attempt to cast to a customTreeNode and just try to pass it to
displayInfo, I get a compilation error because the compiler thinks
that e.Node is TreeNode, not a customTreeNode (expected).

Am I casting incorrectly? Is this a bug? Is the documentation shoddy?

Thanks,
Andrew
Nov 13 '05 #1
7 6143
Andrew <ac*********@pherret.com> wrote:

<snip>
At runtime, I get a InvalidCastException. What gives?

In the AfterSelect event handler method, if I do
'e.Node.GetType()'--it is, in fact, a customTreeNode. If I do not
attempt to cast to a customTreeNode and just try to pass it to
displayInfo, I get a compilation error because the compiler thinks
that e.Node is TreeNode, not a customTreeNode (expected).

Am I casting incorrectly? Is this a bug? Is the documentation shoddy?


Are you loading the custom nodes via reflection by any chance? Many
cast errors which look like they shouldn't happen is because you're
trying to cast an instance of one type to another type with the same
name, but from a different assembly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2
> Are you loading the custom nodes via reflection by any chance? Many
cast errors which look like they shouldn't happen is because you're
trying to cast an instance of one type to another type with the same
name, but from a different assembly.


Not sure what you mean by 'loading'. But, I load the nodes via the Add
method:
myTreeView.Nodes.Add(my_customTreeNode);

Add my customTreeNode class looks like this:

---customTreeNode class---

public customTreeNode : TreeNode {
private customData _myData;

public customTreeNode(customData myData) : base(myData.name,
(int)myData.status, (int)myData.status) { //to init a TreeNode with
different images based on the status
_myData = myData;
}

--end--
Nov 13 '05 #3
Andrew <ac*********@pherret.com> wrote:
Are you loading the custom nodes via reflection by any chance? Many
cast errors which look like they shouldn't happen is because you're
trying to cast an instance of one type to another type with the same
name, but from a different assembly.


Not sure what you mean by 'loading'. But, I load the nodes via the Add
method:
myTreeView.Nodes.Add(my_customTreeNode);

Add my customTreeNode class looks like this:

---customTreeNode class---

public customTreeNode : TreeNode {
private customData _myData;

public customTreeNode(customData myData) : base(myData.name,
(int)myData.status, (int)myData.status) { //to init a TreeNode with
different images based on the status
_myData = myData;
}


Hmmm - that should be fine then. What happens if you do:

Console.WriteLine (e.Node.GetType()==typeof(customTreeNode));

?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #4
> Hmmm - that should be fine then. What happens if you do:

Console.WriteLine (e.Node.GetType()==typeof(customTreeNode));

?


It returns false;
Nov 13 '05 #5
[Please don't mail me and post at the same time - it only confuses
things.]

Andrew <ac*********@pherret.com> wrote:
Hmmm - that should be fine then. What happens if you do:

Console.WriteLine (e.Node.GetType()==typeof(customTreeNode));

?


It returns false;


Right. In that case, check:

o The name (I think you've done this already)
o The assembly each type was loaded from, in terms of filename
o The assembly each type was loaded from, in terms of object reference

Can you provide a short but complete piece of code which demonstrates
the problem (and nothing else)?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #6
Your cast looks good. I use descendants of ListViewItems all the time.
Just got done working on one where ListViewMDBItem descended from
ListViewItem and contained all kinds of interesting tidbits about the
schema in the MDB file as well as the FullName to the mdb file. I've
done bunches of things like this and not had any problem, as long as
I'm watchful of the casts.

What he meant by "loading" your objects via reflection is if you were
sucking them from a DLL at runtime using the activator class. It
sounds like you're not so that problem isn't relavent here. Your code
should run fine.

Am I casting incorrectly? Is this a bug? Is the documentation shoddy?

Thanks,
Andrew

Nov 13 '05 #7
Andrew <ac*********@pherret.com> wrote:
Right. In that case, check:

o The name (I think you've done this already)
o The assembly each type was loaded from, in terms of filename
o The assembly each type was loaded from, in terms of object reference

Can you provide a short but complete piece of code which demonstrates
the problem (and nothing else)?


Yes, I attached the code. I tried to make it as short as possible.


<snip>

On my box that immediately bails out - and e.Node is *not* a
customTreeNode, it's a plain System.Windows.Forms.TreeNode.

That's because the "mommy" node (which is initially selected) is a
plain TreeNode. Changing the code for treeView1_AfterSelect to:

private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
if (e.Node is customTreeNode)
{
customTreeNode cNode = (customTreeNode)e.Node;
this.textBox1.Text = cNode.myData;
}
}

it changes the text when I click on a child node, but not otherwise.

Given what you said previously about e.Node actually *being* a
customTreeNode when it's failing, I don't think I'm seeing the actual
problem here.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #8

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

Similar topics

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...
9
by: Mark Allison | last post by:
Hi there, I'm still very much a newbie to C#, and would like to derive from TreeNode to extend the TreeNode class. Here's test program: http://www.vkarlsen.no/pastebin/default.asp?id=4090 I...
2
by: Benny Raymond | last post by:
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;" Does anyone know what would...
2
by: Brian | last post by:
I define my class as below, I don't need to override the "new(text as string)" method as it not inherited, but I don't know why. ------------------- Public Class TreeNode2 Inherits...
7
by: Bob Hollness | last post by:
Hi all. I know this is easy. What have I missed? I want to iterate through all child nodes from the top. Dim treeVal as TreeNode = Nothing For Each treeVal in MyTree.Nodes if...
11
by: Mano | last post by:
When settiing a TreeNode to bold the text label of that node is clipped. As describedin in the Bug Rebort FDBK16963 I set the underliying TreeView Fond to be bold. Now I have the strange...
7
by: Alan T | last post by:
Which event should I deal with when the user is selecting a node on the list view ?
5
by: Zeppe | last post by:
Hi all! my problem is this one, I think that it could be a common one, maybe a pattern, so if you can help me somehow it would be great. Let's suppose I have a class Base class Base { //...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.