Treeview / Nodes

G

Guest

Hello all,

Ive been using the treeview and for testing purpose i have created 2 Parent
Nodes and 5 child nodes within each parent node i.e.-

CN = Child Node
Parent Node
- CN
- CN
- CN
- CN
- CN
Parent Node 2
- CN
- CN
- CN
- CN
- CN

1. My question is that i have been advised (not from here) using treeview in
this fashion is not recommended as its bad coding or it will create problems
later - is this true?

2. I cant have a code that would perform some kind of task for each of the
child nodes, even if i could there would be too much confusion and not the
correct way of doing things.
i.e.
Treeview1.Node.ChildNode1(process.Start "Name of my process")

- I know the code definition is incorrect here but thats an example (unless
someone knows the proper code)

So what do you guys reckon?

If you think i need some more reading or understanding on nodes and
treeviews, then please do provide further links.

As always thanks in advance.
 
C

Cor Ligthert

LC,

I never did it your way, so some guesing in this answer.

1. My question is that i have been advised (not from here) using treeview
in
this fashion is not recommended as its bad coding or it will create
problems
later - is this true?

I would not know why
2. I cant have a code that would perform some kind of task for each of
the
child nodes, even if i could there would be too much confusion and not the
correct way of doing things.
i.e.
Treeview1.Node.ChildNode1(process.Start "Name of my process")
I had to write I never did that, and to be sure it works I made a sample for
you.

\\\needs only a treeview on a form
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() _
{New System.Windows.Forms.TreeNode("WindowPrograms", _
New System.Windows.Forms.TreeNode() _
{New System.Windows.Forms.TreeNode("Notepad")})})
Me.TreeView1.SelectedImageIndex = -1
TreeView1.Nodes(0).Nodes(0).Tag = "notepad.exe"
End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.TreeViewEventArgs) _
Handles TreeView1.AfterSelect
If Not e.Node.Tag Is Nothing Then
Dim p As New Process
p.StartInfo.UseShellExecute = True
p.StartInfo.FileName = e.Node.Tag.ToString
p.Start()
End If
End Sub
///

I hope this helps?

Cor
 
G

Guest

Thanks Cor, that makes a whole lot more sense. Just curious if i made a
program that may hold a lot of nodes/ child nodes (lets say for example sake
Windows Explorer) would you recommend this approach that is using this type
of code throughout the whole program?

Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() _
{New System.Windows.Forms.TreeNode("WindowPrograms", _
New System.Windows.Forms.TreeNode() _
{New System.Windows.Forms.TreeNode("Notepad")})})
Me.TreeView1.SelectedImageIndex = -1
TreeView1.Nodes(0).Nodes(0).Tag = "notepad.exe"

Again thanks very much for your help.

LC
 
C

Cor Ligthert

LC,

No I would do it like this

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
TreeView1.BeginUpdate()
TreeView1.Nodes.Clear()
For i As Integer = 0 To Whatever from my source
TreeView1.Nodes.Add(New TreeNode("Whatever"))
For y As Integer = 0 To Whatever from my source
TreeView1.Nodes(i).Nodes.Add("Whatever"))
TreeView1.Nodes(i).Nodes(y).Tag = "Whatever"
Next
Next
TreeView1.EndUpdate()
End Sub
///

I hope this helps?

Cor
 

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