get child node of selected parent node

  • Thread starter Thread starter jcoon
  • Start date Start date
J

jcoon

All,

I've tried working on this this before with no luck. I've looked all over
and can't seem to find a sample that I can redo (understand) to test with my
sample.
I've created a treeview with nodes from the sample below. I'd like to be
able to use the TreeView1_AfterSelect to retrieve the child node text values
and tag values.
to place in message box or userform labels.

If I select parent node Visual-Runway-A in the treeview I'd like to collect
the text values and tag values for all nodes(0)...under the parent
Visual-Runway-A. )
Pretty new to dot net.

Thanks for all your help.
John



TreeView1.Nodes.Add("Visual-Runway-A")
TreeView1.Nodes(0).Tag = ("Visual-Runway-A")
TreeView1.Nodes(0).Nodes.Add("Inner Width").Tag = "250"
TreeView1.Nodes(0).Nodes.Add("Outer Width").Tag = "1250"
TreeView1.Nodes(0).Nodes.Add("Surface Length").Tag = "5000"
TreeView1.Nodes(0).Nodes.Add("Slope").Tag = "20:1"
TreeView1.Nodes(0).Nodes.Add("Radius").Tag = "5000"

TreeView1.Nodes.Add("Visual-Runway-B")
TreeView1.Nodes(1).Tag = ("Visual-Runway-B")
TreeView1.Nodes(1).Nodes.Add("Inner Width").Tag = "500"
TreeView1.Nodes(1).Nodes.Add("Outer Width").Tag = "1500"
TreeView1.Nodes(1).Nodes.Add("Surface Length").Tag = "5000"
TreeView1.Nodes(1).Nodes.Add("Slope").Tag = "20:1"
TreeView1.Nodes(1).Nodes.Add("Radius").Tag = "5000"

continues.......to nodes(5) not shown
 
jcoon said:
All,

I've tried working on this this before with no luck. I've looked all
over and can't seem to find a sample that I can redo (understand) to
test with my sample.
I've created a treeview with nodes from the sample below. I'd like to be
able to use the TreeView1_AfterSelect to retrieve the child node text
values and tag values.
to place in message box or userform labels.

If I select parent node Visual-Runway-A in the treeview I'd like to
collect the text values and tag values for all nodes(0)...under the
parent Visual-Runway-A. )
Pretty new to dot net.

Thanks for all your help.
John



TreeView1.Nodes.Add("Visual-Runway-A")
TreeView1.Nodes(0).Tag = ("Visual-Runway-A")
TreeView1.Nodes(0).Nodes.Add("Inner Width").Tag = "250"
TreeView1.Nodes(0).Nodes.Add("Outer Width").Tag = "1250"
TreeView1.Nodes(0).Nodes.Add("Surface Length").Tag = "5000"
TreeView1.Nodes(0).Nodes.Add("Slope").Tag = "20:1"
TreeView1.Nodes(0).Nodes.Add("Radius").Tag = "5000"

TreeView1.Nodes.Add("Visual-Runway-B")
TreeView1.Nodes(1).Tag = ("Visual-Runway-B")
TreeView1.Nodes(1).Nodes.Add("Inner Width").Tag = "500"
TreeView1.Nodes(1).Nodes.Add("Outer Width").Tag = "1500"
TreeView1.Nodes(1).Nodes.Add("Surface Length").Tag = "5000"
TreeView1.Nodes(1).Nodes.Add("Slope").Tag = "20:1"
TreeView1.Nodes(1).Nodes.Add("Radius").Tag = "5000"

continues.......to nodes(5) not shown

It appears you want a VB example, so try this:

Private Sub TreeView1_AfterSelect _
(ByVal sender As Object, _
ByVal e As System.Windows.Forms.TreeViewEventArgs) _
Handles TreeView1.AfterSelect

Dim tn As TreeNode = e.Node
Dim d As New Dictionary(Of String, Object)

For Each child As TreeNode In tn.Nodes
d.Add(child.Text, child.Tag)
Next

' do something with the dictionary
End Sub

e.Node contains the node selected, and the Nodes property contains the
children of that node.
 
Mike,

Thank you, I was able to add your sample and retrieved the child.text and
child.tag under the selected parent.
would I have to do another for each loop to to get the remaining child text
and tags under the parent.

in this sample if I select the parent node Visual-Runway-A
I'd like to get all child text and tag so I can read them into a messagebox

any hints or links are welcome.

have a great day
John


TreeView1.Nodes.Add("Visual-Runway-A")
TreeView1.Nodes(0).Tag = ("Visual-Runway-A")
TreeView1.Nodes(0).Nodes.Add("Inner Width").Tag = "250"
TreeView1.Nodes(0).Nodes.Add("Outer Width").Tag = "1250"
TreeView1.Nodes(0).Nodes.Add("Surface Length").Tag = "5000"
TreeView1.Nodes(0).Nodes.Add("Slope").Tag = "20:1"
TreeView1.Nodes(0).Nodes.Add("Radius").Tag = "5000"

TreeView1.Nodes.Add("Visual-Runway-B")
TreeView1.Nodes(1).Tag = ("Visual-Runway-B")
TreeView1.Nodes(1).Nodes.Add("Inner Width").Tag = "500"
TreeView1.Nodes(1).Nodes.Add("Outer Width").Tag = "1500"
TreeView1.Nodes(1).Nodes.Add("Surface Length").Tag = "5000"
TreeView1.Nodes(1).Nodes.Add("Slope").Tag = "20:1"
TreeView1.Nodes(1).Nodes.Add("Radius").Tag = "5000"
 
jcoon said:
Mike,

Thank you, I was able to add your sample and retrieved the child.text
and child.tag under the selected parent.
would I have to do another for each loop to to get the remaining child
text and tags under the parent.

in this sample if I select the parent node Visual-Runway-A
I'd like to get all child text and tag so I can read them into a messagebox

any hints or links are welcome.

have a great day
John


TreeView1.Nodes.Add("Visual-Runway-A")
TreeView1.Nodes(0).Tag = ("Visual-Runway-A")
TreeView1.Nodes(0).Nodes.Add("Inner Width").Tag = "250"
TreeView1.Nodes(0).Nodes.Add("Outer Width").Tag = "1250"
TreeView1.Nodes(0).Nodes.Add("Surface Length").Tag = "5000"
TreeView1.Nodes(0).Nodes.Add("Slope").Tag = "20:1"
TreeView1.Nodes(0).Nodes.Add("Radius").Tag = "5000"

The code I gave should have given a dictionary with five elements in it.
The keys should have been "Inner Width", "Outer Width", "Surface
Length", "Slope" and "Radius". The values for the keys respectively,
should have been "250", "1250", "5000", "20:1" and "5000".

Are you not getting this from the code when you select "Visual-Runaway-A"?
 
Mike,

Sorry, I now see the dictionary in the locals window under d and it is
cycling thru
the items under the selected parent node. I've never worked with a
dictionary or treeview before, I was looking in the wrong location.

In debug I can see the d.item keys and vlaue string as it iterates thru and
populates the dictionary, but when I try to get the values I don't see how to
get the d.tem key and d.item value string as shown in the locals window.

I know this is a struggle working with someone who doesn't understand these
newer control items but in time I will learn how these tools work, the old me
would of just made and if Visual-Runway-A was selected then form label
1,2,3... are equal to what I already types in the tree text, I'm trying to
change that.

Have a great day,
John
 
jcoon said:
Mike,

Sorry, I now see the dictionary in the locals window under d and it is
cycling thru
the items under the selected parent node. I've never worked with a
dictionary or treeview before, I was looking in the wrong location.

In debug I can see the d.item keys and vlaue string as it iterates thru and
populates the dictionary, but when I try to get the values I don't see how to
get the d.tem key and d.item value string as shown in the locals window.

I know this is a struggle working with someone who doesn't understand these
newer control items but in time I will learn how these tools work, the old me
would of just made and if Visual-Runway-A was selected then form label
1,2,3... are equal to what I already types in the tree text, I'm trying to
change that.

Have a great day,
John

No problem, the environment can take time to get familiar.

Maybe this code is more useful (typed freehand, not compiled):

Private Sub TreeView1_AfterSelect _
(ByVal sender As Object, _
ByVal e As System.Windows.Forms.TreeViewEventArgs) _
Handles TreeView1.AfterSelect

Dim tn As TreeNode = e.Node
' Dim d As New Dictionary(Of String, Object)
Dim sb as New StringBuilder()

For Each child As TreeNode In tn.Nodes
' d.Add(child.Text, child.Tag)
sb.AppendLine(child.Text & ": " & child.Tag.ToString())
Next

MessageBox.Show(sb.ToString())
' do something with the dictionary
End Sub
 
Mike,



I get this error type "stringbuilder" not defined.

I'm using visual basic express 2005, Is that an issue?

Dim sb As New StringBuilder() ,
For Each child As TreeNode In tn.Nodes
sb.AppendLine(child.Text & ": " & child.Tag.ToString())
Next
MessageBox.Show(sb.ToString())
 
jcoon said:
Mike,



I get this error type "stringbuilder" not defined.

I'm using visual basic express 2005, Is that an issue?

Dim sb As New StringBuilder() ,
For Each child As TreeNode In tn.Nodes
sb.AppendLine(child.Text & ": " & child.Tag.ToString())
Next
MessageBox.Show(sb.ToString())

Sorry about that. There is an additional imports statement needed at
the top of your code. Add this line at the very top of your code:

Imports System.Text

It should work fine in VS 2005 Express.
 
Mike,

that was it........I now can play around with both the dictionary and this
stringbuilder for a couple of weeks so I can try to understand how they work
better.

Thank you for all your help and for being so patient.

John
 
Back
Top