Extending the TreeNode - - -> HELP!

D

Don

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 a TreeView or ListView Control"

I've been able to add nodes of myTreeNode type to the treeview and
verify that they really are myTreeNode nodes but haven't been able to
get this part figured out.

Thank you for any help,

Don

The 'CType' line produces the following error:

'System.InvalidCastException'
Additional information: Specified cast is not valid.

Private Sub tvSnips_BeforeExpand(REMOVED) Handles tvSnips.BeforeExpand

Dim mynode As myTreeNode
mynode = CType(e.Node, myTreeNode) <-------ERROR
MessageBox.Show("Node selected is " & mynode.NodeParent)

End Sub


Public Class myTreeNode
Inherits TreeNode
Private mintParent As Integer

Public Enum enumNodeType
Dummy = -1
Group = 0
Leaf = 1
End Enum

Public nt As enumNodeType

Public Property NodeType() As enumNodeType
Get
Return nt
End Get
Set(ByVal Value As enumNodeType)
nt = Value
End Set
End Property

Public Property NodeParent() As Integer
Get
Return mintParent
End Get
Set(ByVal Value As Integer)
mintParent = Value
End Set
End Property
End Class
 
A

Armin Zingler

Don said:
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 a TreeView or ListView Control"

I've been able to add nodes of myTreeNode type to the treeview and
verify that they really are myTreeNode nodes but haven't been able
to get this part figured out.

Thank you for any help,

Don

The 'CType' line produces the following error:

'System.InvalidCastException'
Additional information: Specified cast is not valid.

Private Sub tvSnips_BeforeExpand(REMOVED) Handles
tvSnips.BeforeExpand

Dim mynode As myTreeNode
mynode = CType(e.Node, myTreeNode) <-------ERROR
MessageBox.Show("Node selected is " & mynode.NodeParent)

End Sub


Public Class myTreeNode
[...]


Set a breakpoint at the error line and examine the type of e.Node.


Armin
 
D

Don

Hi Armin,


This is probably more than you want to look at but here it is. Is this
what you had in mind?

I don't understand the ramifications of the difference in
'GetType.BaseType' for e.Node and MyNode.

Do you think any of this sheds any light on what the problem may be?

Thank you,

Don


========= e.Node Stuff ===============

Console.WriteLine("e.node type is " & TypeName(e.Node))

e.node type is TreeNode
---------

e.Node.GetType.BaseType.ToString

e.node type is System.MarshalByRefObject
---------

e.Node.GetType.UnderlyingSystemType.FullName

e.node type is System.Windows.Forms.TreeNode


========= MyNode Stuff ===============

mynode.GetType.BaseType.ToString

mynode type is System.Windows.Forms.TreeNode

---------

Console.WriteLine("mynode type is " & mynode.GetType.FullName)

mynode type is CodeSnippet_Test.myTreeNode





Don said:
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 a TreeView or ListView Control"

I've been able to add nodes of myTreeNode type to the treeview and
verify that they really are myTreeNode nodes but haven't been able
to get this part figured out.

Thank you for any help,

Don

The 'CType' line produces the following error:

'System.InvalidCastException'
Additional information: Specified cast is not valid.

Private Sub tvSnips_BeforeExpand(REMOVED) Handles
tvSnips.BeforeExpand

Dim mynode As myTreeNode
mynode = CType(e.Node, myTreeNode) <-------ERROR
MessageBox.Show("Node selected is " & mynode.NodeParent)

End Sub


Public Class myTreeNode
[...]


Set a breakpoint at the error line and examine the type of e.Node.


Armin
 
A

Armin Zingler

Don said:
Hi Armin,


This is probably more than you want to look at but here it is. Is
this what you had in mind?

I don't understand the ramifications of the difference in
'GetType.BaseType' for e.Node and MyNode.


How can you examine MyNode? I thought you can not assign a value because you
get the exception.

Do you think any of this sheds any light on what the problem may be?

Thank you,

Don


========= e.Node Stuff ===============

Console.WriteLine("e.node type is " & TypeName(e.Node))

e.node type is TreeNode
---------

e.Node.GetType.BaseType.ToString

e.node type is System.MarshalByRefObject
---------

e.Node.GetType.UnderlyingSystemType.FullName

e.node type is System.Windows.Forms.TreeNode


Obviously you did not only add "myTreeNode" objects. Maybe you used the Form
designer to add nodes at design time? If not, review your code to see
whether you also add TreeNode objects instead of myTreeNode objects.

========= MyNode Stuff ===============

mynode.GetType.BaseType.ToString

mynode type is System.Windows.Forms.TreeNode

---------

Console.WriteLine("mynode type is " & mynode.GetType.FullName)

mynode type is CodeSnippet_Test.myTreeNode



Armin
 
D

Don

I Dim'd a New MyTreenode in the 'BeforeExpand' to compare the Type of
a MyTreenode to the e.Node object. This did not cause the problem
though.

Dim MyNode as New MyTreenode

Console.WriteLine("mynode type is " & mynode.GetType.FullName)

I don't believe I added both node types to the tree anywhere in the
code but it's a good suggestion and I'll double check.

Thank you,

Don
 
P

Phill W.

Don said:
I've created a small test class to extend the Treenode object and am
having mixed success. .. . .
I've been able to add nodes of myTreeNode type to the treeview and
verify that they really are myTreeNode nodes but haven't been able to
get this part figured out. .. . .
The 'CType' line produces the following error:

'System.InvalidCastException'
Additional information: Specified cast is not valid.

Dim mynode As myTreeNode
mynode = CType(e.Node, myTreeNode) <-------ERROR

Probably because the above is called for *every* node that wants to
expand, not just yours. Check the Type of the node before casting it,
as in

If TypeOf e.Node Is myTreeNode Then
Dim mynode As myTreeNode
mynode = CType(e.Node, myTreeNode)
. . .
End If

HTH,
Phill W.
 

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