The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > Visual Basic


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: , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 06-27-2007, 07:48 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Icon11 VB.NET 2005 Daily Lesson [6.27.07] - Recursive TreeNode Creation

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
Reply With Quote
The Following User Says Thank You to ccoonen For This Useful Post:
HelloWorld (06-27-2007)
  #2 (permalink)  
Old 06-27-2007, 04:50 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Thanx for the nice tutorial, even I won't actually try it until I'm done with C#.NET

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 06-27-2007, 07:35 PM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
No problem, hope it helps some people out... it can be used with anything to create a recursive structure
Reply With Quote
  #4 (permalink)  
Old 06-27-2007, 08:20 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Originally Posted by ccoonen View Post
No problem, hope it helps some people out... it can be used with anything to create a recursive structure
Why do you think that Recursion is the only way to go? Can't we go with the Iterative way? Since, it's just basically the same thing.. however, I'm pretty sure that Iterative way is going to be much more complicated...?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 06-27-2007, 08:46 PM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
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
That's 3 levels deep. With recursion it can go Unlimited levels deep. You can have parent, child, child, child, child, child, child, etc... iinfinite levels (n-level). This is why recursion is the only way because their is not telling how "deep" the levels go
Reply With Quote
The Following User Says Thank You to ccoonen For This Useful Post:
HelloWorld (06-27-2007)
  #6 (permalink)  
Old 06-27-2007, 09:57 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
oh yeah, that's pretty true.......... thanx, nice thing to remember my broken mind.. lol...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 05:01 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50