Treeviews

Questions regarding syntax

Treeviews

Postby gbison » Fri Oct 21, 2011 7:49 pm

Hey guys,

Im having a little bit of a time understanding the tree view system. What im doing is working on a tiny program that helps with file managment. For example, you add directories to the tree view when you add a directory, it does a search within that directory and adds all the file contents as children under the directory name. All that seems to work fine. The problem Im having is identifying the ID's associated with the directories and it seem every command thats worth anything for the treeview object is requireing these. I have looked, looked and relooked at the reference for this object to find something that will give me a valid ID for the top level item. Unless I capture this value in a variable when i first create it, I cant seem to figure out how to get it.

Another example, I add a directory to the list, it then populates all the files within that directory as children of that directory in the tree view. Now lets say I want to add another random file to that directory from within the tree view, well to add a file to a specific item as a child you need its ID and I can find no way to retrieve this? Im sure im looking over the obvious but if someone could point me in the right direction here it would sure help.

Thanks
"The greatest trick the devil ever pulled was to convince the world he didn't exist" . ;-)
gbison
 
Posts: 29
Joined: Fri Feb 04, 2011 8:16 pm
Location: USA

Re: Treeviews

Postby berndnoetscher » Mon Oct 24, 2011 7:26 am

This might help you:

Finding an item returns the id of that item.
http://www.kbasic.com/doku.php?id=treeview#finditem

Or using overall first item (InvisibleRootItem)

Deleting all items:

Code: Select all
Private Sub CommandButton11_OnEvent()
  Dim id As Integer = TreeView1.InvisibleRootItem()

  Dim n As Integer = TreeView1.ChildCount(id)
  Dim child As Integer
 
  For i As Integer = 1 To n
   
    child = TreeView1.Child(id, i)
   
    If TreeView1.ChildCount(child) > 0 Then DeleteTreeViewItem(child)
   
    TreeView1.RemoveChild(id, i)
    n = n - 1
    i = i - 1
   
  Next
End Sub
berndnoetscher
Site Admin
 
Posts: 1059
Joined: Tue Sep 25, 2007 9:37 am

Re: Treeviews

Postby Henning » Mon Oct 24, 2011 11:55 am

Hi Bernd,

As beeing completely against changing the loop index or loop count inside a For/Next loop, use another loop or do it in reverse.

In your example each iteration removes the first child with i = 1, and the loop count is decremented to compensate. That is because i is incremented again in the Next statement.

Code: Select all
    Private Sub CommandButton11_OnEvent()
      Dim id As Integer = TreeView1.InvisibleRootItem()

      Dim n As Integer = TreeView1.ChildCount(id)
      Dim child As Integer
     
      For i As Integer = n To 1 Step -1
       
        child = TreeView1.Child(id, i)
       
        If TreeView1.ChildCount(child) > 0 Then DeleteTreeViewItem(child)
       
        TreeView1.RemoveChild(id, i)
       
      Next
    End Sub


Not tested, but I guess it will produce the same result without fiddeling with loop index or count.

/Henning
Henning
 
Posts: 262
Joined: Mon Feb 09, 2009 12:03 am
Location: Sweden

Re: Treeviews

Postby Henning » Mon Oct 24, 2011 5:12 pm

Hi again,

Another thing is that removing from the end, doesn't make the TreeView rearrange indexes 2 to n to new 1 to n-1 in every loop. ;)

/Henning
Henning
 
Posts: 262
Joined: Mon Feb 09, 2009 12:03 am
Location: Sweden

Re: Treeviews

Postby gbison » Tue Oct 25, 2011 8:24 pm

thanks guys, I'll have a go at it. I have actually setup a method for storing root id's via an array thats assigned to the childs tag value during its creation. This allows me to work faster through things by referencing the childs ID tag to know who its parent is. This way if I need the parents ID to remove the child its a simple tag reference on the child. I created two seperate functions that handle getting ids for both the child and the parrent. So now my call to remove a child looks like this TreeView1.RemoveChild(ids[getParentID()], getChildID()) .

I have tested this new creation and it works flawlessly thus far and very fast. :)
"The greatest trick the devil ever pulled was to convince the world he didn't exist" . ;-)
gbison
 
Posts: 29
Joined: Fri Feb 04, 2011 8:16 pm
Location: USA


Return to Coding Questions

cron