Connecting Tech Pros Worldwide Help | Site Map

Inheriting the TreeView problem...

  #1  
Old November 21st, 2005, 11:12 AM
johnb41
Guest
 
Posts: n/a
In my treeview, I need each node to hold a "value", not just
text/label. Kind of like how a ListBox item has text, and a value
associated with it. I need this because when a node is clicked, i need
to get the "value" behind the node and then use it do do something.

Anyway, I made a custom treenode class that inherits from Treenode, and
added a property called "value".

I was able to successfully create the Treeview programmatically from a
database, which includes the "value" property that i made.

Here's my problem: The routine I have to run when a user clicks a node
is this:

Private Sub treeview_AfterSelect(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles
treeview.AfterSelect
'do something with e.Node.value
End Sub

Because my "value" property was added in my custom class, it is not
available to use in TreeViewEventArgs. So e.Node.Value will not work.
How can I do this?...i'm stumped!

Thanks for your help!
John

  #2  
Old November 21st, 2005, 11:12 AM
Samuel R. Neff
Guest
 
Posts: n/a

re: Inheriting the TreeView problem...



You can cast it.

DirectCast(e.Node, MyCustomTreeNode).Value

btw, perhaps you use the Tag property instead of a custom class?

HTH,

Sam


On 26 Jan 2005 07:38:21 -0800, "johnb41" <orders@informatik.com>
wrote:
[color=blue]
>In my treeview, I need each node to hold a "value", not just
>text/label. Kind of like how a ListBox item has text, and a value
>associated with it. I need this because when a node is clicked, i need
>to get the "value" behind the node and then use it do do something.
>
>Anyway, I made a custom treenode class that inherits from Treenode, and
>added a property called "value".
>
>I was able to successfully create the Treeview programmatically from a
>database, which includes the "value" property that i made.
>
>Here's my problem: The routine I have to run when a user clicks a node
>is this:
>
>Private Sub treeview_AfterSelect(ByVal sender As System.Object,
>ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles
>treeview.AfterSelect
>'do something with e.Node.value
>End Sub
>
>Because my "value" property was added in my custom class, it is not
>available to use in TreeViewEventArgs. So e.Node.Value will not work.
>How can I do this?...i'm stumped!
>
>Thanks for your help!
>John[/color]

  #3  
Old November 21st, 2005, 11:12 AM
Peter Proost
Guest
 
Posts: n/a

re: Inheriting the TreeView problem...


Quick and dirty, the important thing is the directcast in the afterselect
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
_ Handles MyBase.Load
TreeView1.Nodes.Add(New MyTreeNode("Belgium", "BE"))
TreeView1.Nodes.Add(New MyTreeNode("Netherlands", "NL"))
TreeView1.Nodes.Add(New MyTreeNode("France", "FR"))
TreeView1.Nodes.Add(New MyTreeNode("Germany", "GER"))
End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As _
System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
MsgBox(DirectCast(TreeView1.SelectedNode, MyTreeNode).value)
End Sub

Public Class MyTreeNode
Inherits TreeNode
Private myvalue As String
Public Property value() As String
Get
Return myvalue
End Get
Set(ByVal Value As String)
myvalue = Value
End Set
End Property
Sub New(ByVal text As String, ByVal value As String)
MyBase.New()
Me.Text = text
Me.value = value
End Sub

End Class

hth Greetz Peter


"Samuel R. Neff" <blinex@newsgroup.nospam> wrote in message
news:chefv09q4e5dcqde04qrekgv591udm9o94@4ax.com...[color=blue]
>
> You can cast it.
>
> DirectCast(e.Node, MyCustomTreeNode).Value
>
> btw, perhaps you use the Tag property instead of a custom class?
>
> HTH,
>
> Sam
>
>
> On 26 Jan 2005 07:38:21 -0800, "johnb41" <orders@informatik.com>
> wrote:
>[color=green]
> >In my treeview, I need each node to hold a "value", not just
> >text/label. Kind of like how a ListBox item has text, and a value
> >associated with it. I need this because when a node is clicked, i need
> >to get the "value" behind the node and then use it do do something.
> >
> >Anyway, I made a custom treenode class that inherits from Treenode, and
> >added a property called "value".
> >
> >I was able to successfully create the Treeview programmatically from a
> >database, which includes the "value" property that i made.
> >
> >Here's my problem: The routine I have to run when a user clicks a node
> >is this:
> >
> >Private Sub treeview_AfterSelect(ByVal sender As System.Object,
> >ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles
> >treeview.AfterSelect
> >'do something with e.Node.value
> >End Sub
> >
> >Because my "value" property was added in my custom class, it is not
> >available to use in TreeViewEventArgs. So e.Node.Value will not work.
> >How can I do this?...i'm stumped!
> >
> >Thanks for your help!
> >John[/color]
>[/color]


  #4  
Old November 21st, 2005, 11:12 AM
johnb41
Guest
 
Posts: n/a

re: Inheriting the TreeView problem...


Thanks Samuel and Peter!

The DirectCast thing did the trick. Amazing. I thought the fix would
need a large amount of code.

Thanks again!
John

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
TreeView multiple selections =?Utf-8?B?TGkgV2VuZw==?= answers 18 November 1st, 2007 04:35 AM
Treeview with ImageList... paradox answers 1 November 16th, 2005 09:24 PM
TreeView and TreeNode problem pei_world answers 2 November 15th, 2005 05:04 PM
class hierarchy problem (do I need MI?) Bonj answers 2 July 23rd, 2005 03:11 AM