VB.Net Treeviews

  • Thread starter Thread starter Rob Oldfield
  • Start date Start date
R

Rob Oldfield

I've been searching around for a good example of how to build/maintain
Treeviews but haven't managed to find one. Could anyone point me at a good
one please.

Windows forms application using VS 2005.
 
Rob said:
I've been searching around for a good example of how to
build/maintain Treeviews but haven't managed to find one. Could
anyone point me at a good one please.

You ARE a programmer, ain't you?

MH
 
Hi Rob,

I suggest that you use a List(Of T) to contain objects and build a TreeView
based on the list. Use a Hashtable to maintain the relationship between
each object's key and the corresponding TreeNode, so you can get the
TreeNode corresponding to a given key randomly.

The following is a sample. It requires that you add a TreeView and two
Buttons on the Form.

Public Class Category
Private m_ID As Integer
Private m_Name As String
Private m_Parent As Integer
Public Property ID()
Get
Return m_ID
End Get
Set(ByVal value)
m_ID = value
End Set
End Property
Public Property Name()
Get
Return m_Name
End Get
Set(ByVal value)
m_Name = value
End Set
End Property
Public Property Parent()
Get
Return m_Parent
End Get
Set(ByVal value)
m_Parent = value
End Set
End Property
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal parent
As Integer)
Me.m_ID = id
Me.m_Name = name
Me.m_Parent = parent
End Sub
End Class

Imports System.Collections

Public Class Form1

Private lists As New List(Of Category)
Private hash As New Hashtable

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lists.Add(New Category(1, "category 1", 0))
lists.Add(New Category(2, "category 2", 0))
lists.Add(New Category(3, "category 3", 0))
lists.Add(New Category(4, "category 1-1", 1))
lists.Add(New Category(5, "category 1-2", 1))
lists.Add(New Category(6, "category 2-1", 2))
lists.Add(New Category(7, "category 3-1", 3))

BuildTreeView(lists)
End Sub

' build a TreeView according to the passed lists
Public Sub BuildTreeView(ByRef lists As List(Of Category))
Dim children As List(Of Category) = GetChildren(lists, 0)
Dim node As TreeNode
While (children.Count > 0)
If (Me.TreeView1.Nodes.Count = 0) Then
For Each entry As Category In children
node = Me.TreeView1.Nodes.Add(entry.Name)
node.Tag = entry.ID
hash.Add(entry.ID, node)
Next
Else
Dim parentnode As TreeNode = node
For Each entry As Category In children
node = parentnode.Nodes.Add(entry.Name)
node.Tag = entry.ID
hash.Add(entry.ID, node)
Next
End If

children = GetChildren(lists, node.Tag)
While (children.Count = 0)
While (node IsNot Nothing)
If (node.PrevNode IsNot Nothing) Then
node = node.PrevNode
Exit While
ElseIf (node.Parent IsNot Nothing) Then
node = node.Parent
Else
node = Nothing
End If
End While
If (node Is Nothing) Then
Exit Sub
End If
children = GetChildren(lists, node.Tag)
End While
End While
End Sub

' get all children of a specified key
Private Function GetChildren(ByRef lists As List(Of Category), ByVal
parent As Integer) As List(Of Category)
Dim children As New List(Of Category)
For Each entry As Category In lists
If entry.Parent = Parent Then
children.Add(entry)
End If
Next
Return children
End Function

' add a new TreeNode to the TreeView and update the Hashtable.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim node As TreeNode = hash(2)
Dim newnode As TreeNode
If (node IsNot Nothing) Then
newnode = node.Nodes.Add("category 2-2")
newnode.Tag = 8
hash.Add(8, newnode)
End If
End Sub
' remove a TreeNode from the TreeView and update the Hashtable
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.TreeView1.Nodes.Remove(hash(7))
hash.Remove(7)
End Sub
End Class

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Linda said:
I suggest that you use a List(Of T) to contain objects and build a
TreeView based on the list. Use a Hashtable to maintain the
relationship between each object's key and the corresponding
TreeNode, so you can get the TreeNode corresponding to a given key
randomly.

Hashtable? OMG!!! Is it a new MS "best practice"?
Linda, did you forget List<> has Find and Exists?

MH
 
Linda Liu said:
Hi Rob,

I suggest that you use a List(Of T) to contain objects and build a TreeView
based on the list. Use a Hashtable to maintain the relationship between
each object's key and the corresponding TreeNode, so you can get the
TreeNode corresponding to a given key randomly.

The following is a sample. It requires that you add a TreeView and two
Buttons on the Form.

<snip>

Thanks for that Linda. Gives me a good starting point I think. I'll just
have to live with the fact that Mr Geezer seems to have taken a dislike to
me. You think there's anything in the framework that might help his social
skills or understanding of what newsgroups are for?
 
Rob said:
I'll just have to live with the fact that Mr Geezer seems to have
taken a dislike to me.

I don't like nor dislike you. I just think you may not have enough
qualities to be a good programmer. Any average programmer should figure
out your "problem" without any help.

Also I don't think Linda is a good "teacher".

Let's look at her example.
It's based on assumption that an access is needed to nodes by providing
some kind of ID. What she does? She uses Hashtable. But we are in age
of OOP. It's enough to add TreeNode to Category and use Find to find an
item (and node). No need for Hashtable. I can only assume that she does
not know how to use Find (MS example is useless).

MH
 
Hi Rob,

Thank you for your response!

If you have anything unclear, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

To Geezer,
I just think you may not have enough qualities to be a good programmer.
Any average programmer should figure out your "problem" without any help.

I'm afraid that I couldn't agree with you. Maybe you're a person who always
tries to solve everything by himself, but you shouldn't or needn't require
other people to be the same as you : )

Every experienced or senior programmer grows up from a junior programmer
and a good start is a half of success. I believe Rob will make progress
more rapidly with the help of the newsgroups.
It's enough to add TreeNode to Category and use Find to find an item (and
node). No need for Hashtable.

Yes, we can do that. But I don't think it's appropriate to add a TreeNode
to its corresponding Category object. What if I want to remove an object
from the List(Of T) but don't want to remove the corresponding TreeNode
from the TreeView?

In addition, using a Hashtable to access an object with given key is more
convenient than using the List(Of T).Find method to do the same thing.

Thank you for your suggestion any way. At least, it's an alternative.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Linda said:
Yes, we can do that. But I don't think it's appropriate to add a
TreeNode to its corresponding Category object. What if I want to
remove an object from the List(Of T) but don't want to remove the
corresponding TreeNode from the TreeView?

In such case, why do you want to have such link in the first place?
In addition, using a Hashtable to access an object with given key is
more convenient than using the List(Of T).Find method to do the same
thing.

More convinient for whom? Maybe for people who don't know and don't
want to know how to use Find. But on other hand if you are right I
wonder what idiot (and why) created Find for List.

MH
 
Hi Greezer,
But on other hand if you are right I wonder what idiot (and why) created
Find for List.

The Find method of the List(Of T) class uses a delegate to find the first
element that matches the conditions defined by the specified predicate. And
this is a more flexible way to find an element from a list.

Sincerely,
Linda Liu
Microsoft Online Community Support

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