Inherits - TreeNode Control 
Author Message
 Inherits - TreeNode Control

Hai Everyone...

I having problem with the class derived from the TreeNode,

Class called spkNode
   Inherits System.Windows.Forms.TreeNode
End Class

The following code does not work, I get System.InvalidCastException,
additional info : Specified Cast is not valid,

        Dim objNode As New spkNode()
        objNode = TreeView1.Nodes.Add("ssi")
        Dim i As Integer
        For i = 0 To 6
            objNode.Text = "ssi = " & i
            objNode = objNode.Nodes.Add(objNode.Text)
        Next

but the same code with {*filter*}ode, work properly,
        Dim objNode As New TreeNode()
        objNode = TreeView1.Nodes.Add("ssi")
        Dim i As Integer
        For i = 0 To 6
            objNode.Text = "ssi = " & i
            objNode = objNode.Nodes.Add(objNode.Text)
        Next

Am I missing some thing here, if my class works I need to add few
properties, any suggestions greatly appreciated.

Thanks in advance,
Sudhakar



Sat, 05 Feb 2005 07:12:12 GMT  
 Inherits - TreeNode Control
The problem (as indicated below) is that you're trying to "downcast" when
that isn't valid. The return type from the .Add method is TreeNode, not
spkNode, so casting in that direction isn't allowed. I think what you want
do is this

Class spkNode
    Inherits TreeNode

    Public Sub New(ByVal text As String)
        MyBase.New(text)
    End Sub
End Class

then when you want to use it...

Dim myNode As New spkNode("Hello, world")
TreeView1.Nodes.Add(myNode)
For i As Integer = 0 To 6
    myNode = New spkNode("i = " & i)
    TreeView1.Nodes.Add(myNode)
Next i

This posting is provided "AS IS" with no warranties, and confers no rights.



Quote:
> Hai Everyone...

> I having problem with the class derived from the TreeNode,

> Class called spkNode
>    Inherits System.Windows.Forms.TreeNode
> End Class

> The following code does not work, I get System.InvalidCastException,
> additional info : Specified Cast is not valid,

>         Dim objNode As New spkNode()

the cast below here is not valid because the return type is not the same as
the type of objNode
Quote:
>         objNode = TreeView1.Nodes.Add("ssi")
>         Dim i As Integer
>         For i = 0 To 6
>             objNode.Text = "ssi = " & i
>             objNode = objNode.Nodes.Add(objNode.Text)
>         Next

> but the same code with {*filter*}ode, work properly,
>         Dim objNode As New TreeNode()
>         objNode = TreeView1.Nodes.Add("ssi")
>         Dim i As Integer
>         For i = 0 To 6
>             objNode.Text = "ssi = " & i
>             objNode = objNode.Nodes.Add(objNode.Text)
>         Next

> Am I missing some thing here, if my class works I need to add few
> properties, any suggestions greatly appreciated.

> Thanks in advance,
> Sudhakar



Sat, 05 Feb 2005 09:32:45 GMT  
 Inherits - TreeNode Control
Ryan,

Thanks for your solution, but I need little more help,

How can I make my class spkNode to return spkNode from the .Add
method, I want spkNode class to work as like TreeNode does,

My requirement here is that, I read the data from the database and
create the node with the 'Title' and store the 'ProgramName' &
'ProgramType' in properties, if it is a parent node, I need to add
child node to it and it goes on and on.

Please help me,
Thanks in advance,
Sudhakar


Quote:
> The problem (as indicated below) is that you're trying to "downcast" when
> that isn't valid. The return type from the .Add method is TreeNode, not
> spkNode, so casting in that direction isn't allowed. I think what you want
> do is this

> Class spkNode
>     Inherits TreeNode

>     Public Sub New(ByVal text As String)
>         MyBase.New(text)
>     End Sub
> End Class

> then when you want to use it...

> Dim myNode As New spkNode("Hello, world")
> TreeView1.Nodes.Add(myNode)
> For i As Integer = 0 To 6
>     myNode = New spkNode("i = " & i)
>     TreeView1.Nodes.Add(myNode)
> Next i

> This posting is provided "AS IS" with no warranties, and confers no rights.



> > Hai Everyone...

> > I having problem with the class derived from the TreeNode,

> > Class called spkNode
> >    Inherits System.Windows.Forms.TreeNode
> > End Class

> > The following code does not work, I get System.InvalidCastException,
> > additional info : Specified Cast is not valid,

> >         Dim objNode As New spkNode()
> the cast below here is not valid because the return type is not the same as
> the type of objNode
> >         objNode = TreeView1.Nodes.Add("ssi")
> >         Dim i As Integer
> >         For i = 0 To 6
> >             objNode.Text = "ssi = " & i
> >             objNode = objNode.Nodes.Add(objNode.Text)
> >         Next

> > but the same code with {*filter*}ode, work properly,
> >         Dim objNode As New TreeNode()
> >         objNode = TreeView1.Nodes.Add("ssi")
> >         Dim i As Integer
> >         For i = 0 To 6
> >             objNode.Text = "ssi = " & i
> >             objNode = objNode.Nodes.Add(objNode.Text)
> >         Next

> > Am I missing some thing here, if my class works I need to add few
> > properties, any suggestions greatly appreciated.

> > Thanks in advance,
> > Sudhakar



Sun, 06 Feb 2005 08:59:06 GMT  
 Inherits - TreeNode Control

Quote:
> Thanks for your solution, but I need little more help,

> How can I make my class spkNode to return spkNode from the .Add
> method, I want spkNode class to work as like TreeNode does,

If you add the nodes like Ryan showed, you can assign the nodes in the
treeview to a variable of your own TreeNode type using type casting:

Dim spkNode As spkNode
spkNode = DirectCast(Treeview.Nodes(1), spkNode)

Armin



Sun, 06 Feb 2005 21:15:29 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Controls Inheriting Other Control's Properties

2. Controls Inheriting Other Control's Properties

3. Controls Inheriting Other Control's Properties

4. Controls Inheriting Other Control's Properties

5. custom form control: Cannot view design view of forms that inherit this form

6. Tab Control in inherited forms

7. Inheriting from controls

8. Inherited form and remembering controls

9. inherited control question

10. Move controls on inherited form?

11. Creating/Inheriting WinForm controls

12. Inherited Forms, Reseting Controls

 

 
Powered by phpBB® Forum Software