473,809 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting to a derived TreeNode object (from a TreeViewEventAr gs Node)

created a custom class that is derived from TreeNode, let's call it
customTreeNode. I'm trying to use the TreeViewEventAr gs (for the
AfterSelect event) but I cannot cast to my derived TreeNode. Here is
a snip...

---code snip---
private void _TreeView_After Select(object sender, TreeViewEventAr gs e)
{
customTreeNode foo = (customTreeNode )e.Node;
displayInfo(foo );
}

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

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

At runtime, I get a InvalidCastExce ption. 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 6180
Andrew <ac*********@ph erret.com> wrote:

<snip>
At runtime, I get a InvalidCastExce ption. 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.co m>
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.Node s.Add(my_custom TreeNode);

Add my customTreeNode class looks like this:

---customTreeNode class---

public customTreeNode : TreeNode {
private customData _myData;

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

--end--
Nov 13 '05 #3
Andrew <ac*********@ph erret.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.Node s.Add(my_custom TreeNode);

Add my customTreeNode class looks like this:

---customTreeNode class---

public customTreeNode : TreeNode {
private customData _myData;

public customTreeNode( customData myData) : base(myData.nam e,
(int)myData.sta tus, (int)myData.sta tus) { //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.WriteLi ne (e.Node.GetType ()==typeof(cust omTreeNode));

?

--
Jon Skeet - <sk***@pobox.co m>
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.WriteLi ne (e.Node.GetType ()==typeof(cust omTreeNode));

?


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

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

Console.WriteLi ne (e.Node.GetType ()==typeof(cust omTreeNode));

?


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.co m>
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*********@ph erret.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_After Select to:

private void treeView1_After Select(object sender,
System.Windows. Forms.TreeViewE ventArgs e)
{
if (e.Node is customTreeNode)
{
customTreeNode cNode = (customTreeNode )e.Node;
this.textBox1.T ext = 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.co m>
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
5441
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 Clone() method) method but with no success. The cloned object seems to be correctly created but I cannot add it to the treeview (I get no error message but simple the new node isn't added).
9
12727
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 get an error on line 0077 saying: "An unhandled exception of type 'System.InvalidCastException' occurred
2
2408
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 cause this? I just want to be able to use my own node class so that I can store extra data... =========================
2
1472
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 System.Windows.Forms.TreeNode
7
4951
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 treeVal.Checked = True then MsgBox(treeVal.Text) Next
11
4764
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 behavior, if I delete all childnodes, when the parent node is expanted, and rebuild all child treenodes, I can make an childnode bold without clipping it. If I make the same with an closed parent node so after rebulding all childnodes and expanting it,...
7
1717
by: Alan T | last post by:
Which event should I deal with when the user is selecting a node on the list view ?
5
2044
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 { // impl. };
0
3315
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. Provide following view properties to listview, through View menu a. Tile b. Icon
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7662
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.