![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: recursive, tree, treenode, vbnet |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| If you are ever working with a TreeView control, which is what Windows Explorer uses to explore Files and Folders, then you will realize that recursion is the only way to go. The TreeView control uses a TreeNode as a datasource and is n-level with infinite parent, children, nodes, leaves etc... Here is a function that will build your TreeNode recursively by taking a ParentNode as a treenode, adds a node, finds all children, adds all children, and repeat. This is built off a Strongly Typed Dataset with an ID field that link to it's parent in the same field. For example, Fishing would have ID 5 and a ParentID of 0... Fishing would be a Root Item, but Fly Fishing would have a ParentID of 5 so it is a child of Fishing. Code: Private Shared Function GetAllTreeNodes(ByRef CategoryItem As Category, ByRef ParentNode As TreeNode) As TreeNode
Dim NewParentNode As New TreeNode
If ParentNode.Text = "" Then
NewParentNode = ParentNode.Nodes.Add("ROOT_LEVEL_NODE", " Categories")
NewParentNode.Tag = -1
Else
NewParentNode = ParentNode.Nodes.Add(CategoryItem.CategoryID, CategoryItem.Name)
NewParentNode.Tag = CategoryItem.CategoryID
End If
Using CategoryAdapter As New DAL.TableAdapters.CategoryTableAdapter()
Using CategoryTable As DAL.CategoryDataTable = CategoryAdapter.GetByParentCategoryID(CategoryItem.CategoryID)
For Each CategoryRow As DAL.CategoryRow In CategoryTable
GetAllTreeNodes(ConstructObjFromDataRow(CategoryRow), NewParentNode)
Next
End Using
End Using
Return NewParentNode
End Function __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| The Following User Says Thank You to ccoonen For This Useful Post: | ||
HelloWorld (06-27-2007) | ||
| |
| |||
| No problem, hope it helps some people out... it can be used with anything to create a recursive structure ![]() __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| |||
| well... you can iterate to how ever many levels you program it for you know? Code: If you say:
for x = 0 to ubound(datasource)
for y = 0 to ubound(datasource(x))
for z = 0 to ubound(datasource(x)(y))
......
next
next
next ![]() __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| The Following User Says Thank You to ccoonen For This Useful Post: | ||
HelloWorld (06-27-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |