Home All Groups Group Topic Archive Search About

Extending the TreeNode - - -> HELP!

Author
8 Mar 2006 7:29 PM
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

Author
8 Mar 2006 9:17 PM
Armin Zingler
Show quote Hide quote
"Don" <don81846@NO_CaCa.Earthlink.net> schrieb
>
> 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
Author
8 Mar 2006 10:58 PM
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





On Wed, 8 Mar 2006 22:17:22 +0100, "Armin Zingler"
<az.nospam@freenet.de> wrote:

Show quoteHide quote
>"Don" <don81846@NO_CaCa.Earthlink.net> schrieb
>>
>> 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
Author
9 Mar 2006 10:28 AM
Armin Zingler
"Don" <don81846@NO_CaCa.Earthlink.net> schrieb
>
> 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.


Show quoteHide quote
> 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
Author
9 Mar 2006 9:40 PM
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

On Thu, 9 Mar 2006 11:28:53 +0100, "Armin Zingler"
<az.nospam@freenet.de> wrote:

Show quoteHide quote
>"Don" <don81846@NO_CaCa.Earthlink.net> schrieb
>>
>> 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
Author
13 Mar 2006 5:11 PM
Phill W.
Show quote Hide quote
"Don" <don81846@NO_CaCa.Earthlink.net> wrote in message
news:64cu0215va4582srn2fdl1vbaor1aq4gtp@4ax.com...
>
> 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.