Results 1 to 19 of 19

Thread: Treeview Key Property alternatives? (Half Resolved)

  1. #1

    Thread Starter
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    Angry Treeview Key Property alternatives? (Half Resolved)

    Is anyone else as fustrated as I am with the removal of the "key" property off of the Node object? What alternatives have you been using?

    I thought about using the Tag property, but that doesn't really accomplish much because I can not retrieve the node by the Tag property, unless of course if I loop through each node to determine where it is and grab the index. This is not very efficient obviously.

    Say for example you had to load a ton of data, maybe an entire domain of more than 25,000 users plus security groups each user is attached to (not what I'm doing but something done in the past). Hopefully you can see where this would be a problem.

    Anybody have some good ways around this. I've seen a few people creating their own class that is inheriting either the node object or the tree object but none of them seem to be for this specific problem.

    Thanks for any insight on what you are doing.
    Last edited by RealisticGraphics; Mar 21st, 2004 at 06:29 PM.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  2. #2

    Thread Starter
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    My slow, but working solution...

    This is my working, although too slow in most cases, solution to my problem. I created the following function that excepts 2 parameters ndc (TreeNodeCollection) and iIndex (Integer). ndc is the obviously the Node Collection to start the search at (or when it's calling itself, the current Node Collection to be searched) and iIndex is the index I'm searching for (it could be changed to a string if you were storing some other value). The function returns a TreeNode object. Anyway, heres the code.

    VB Code:
    1. Friend Function GetNodeIndex(ByRef ndc As TreeNodeCollection, ByVal iIndex As Integer) As TreeNode
    2.         Dim nd As TreeNode
    3.         For Each nd In ndc
    4.             If nd.Tag = iIndex Then Return nd
    5.             Dim snd As TreeNode = GetNodeIndex(nd.Nodes, iIndex)
    6.             If Not IsNothing(snd) Then Return snd
    7.         Next
    8.     End Function

    When I first ran this, I got several StackOverload Errors, but I've had none since that first attempt. Keep in mind however, I changed no code they just stopped. Feel free to use it or speed it up if you know a way (just let me know how to speed it up if you do )
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Depending on what you are doing you can use a Hashtable to store a key and node pair. Then when you need a node you can retrieve it from the hashtable via the key.

  4. #4

    Thread Starter
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    Could you elaborate?
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here you go!

  6. #6

    Thread Starter
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    Thanks I'll take a look at it.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  7. #7
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    hello there
    edneeis, i downloaded your sample also, it revealed a lot to me, but i have one question, in my code i used as in your example hastables but all this seems to stop working when i use drag-n-drop on the treenodes. here is my example:

    this is where i add nodes:

    VB Code:
    1. Friend AList As New Hashtable()
    2.     Friend IDList As New Hashtable()
    3.  
    4. private sub addnodes(byval name as string, byval employeeID as integer, byval superiorId as integer , byval job as string)
    5.  Dim newNode As New TreeNode(name)
    6.             newNode.Tag = job
    7.             newNode.ImageIndex = 1
    8.             newNode.SelectedImageIndex = 1
    9.             AList.Add(employeeID, newNode)
    10.             IDList.Add(newNode, employeeID)
    11.             ' i need the two hastables so i can 1) retrieve a node by id and 2) get the id of the node via selectednode
    12.             CType(AList(superiorID), TreeNode).Nodes.Add(newNode)
    13.            myTree.ExpandAll()
    14. end sub

    this is dragdrop:

    VB Code:
    1. Private Sub MyTree_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Mytree.DragDrop
    2.         Dim pt As Point
    3.         Dim tnDestination As TreeNode
    4.         Dim tnNew As TreeNode
    5.         '  Dim tnSourceParent As TreeNode
    6.         Dim tnewclone As TreeNode
    7.         Dim oldnodeID As Integer
    8.         If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then
    9.             pt = MyTree.PointToClient(New Point(e.X, e.Y))
    10.             tnDestination = MyTree.GetNodeAt(pt)
    11.                tnNew = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
    12.                 tnewclone = CType(tnNew.Clone, TreeNode)
    13.                 oldnodeID = IDList(tnNew)
    14.                 AList.Remove(oldnodeID)
    15.                 IDList.Remove(tnNew)
    16.                 tnNew.Remove()
    17.                 tnDestination.Nodes.Add(tnewclone)
    18.                 AList.Add(oldnodeID, tnewclone)
    19.                 IDList.Add(tnewclone, oldnodeID)
    20.                 tnDestination.ExpandAll()
    21.                 MyTree.SelectedNode = tnewclone
    22.                draggedNode = Nothing
    23.         End If
    24.     End Sub

    curiously after this the alist hastable remains in good shape but the idlist returns for all the nodes that have been moved just 0 value.
    any ideea how i can save the changes that occur during cloning process to my hashables correctley?
    someone has suggested i do this by recursive method, but that didn't ring any bell to me.
    ehmm...

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'm not clear on what you are trying to do. Are you moving a treenode from one tree to another? Are you copying a treenode by dragging it? Where are you testing to see that the ID is always 0? When the DragDrop code runs are you checking that oldnodeID's value is not 0?

  9. #9
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    what i do is changing the employee structure by drag and drop that is if i drag an employee to another emoployee, the dragged employee and its subemployees(child nodes) become subemployees of the employee i drag to. i found a sample for drag and drop and it works just fine. the hastables is what i don't know how to work with

    here is the procedure i use to read the tree. this is just for debugging as i will later add entries to a database with the 4 values for each emplyee:
    VB Code:
    1. Private Sub readtree()
    2.         Dim idA As Integer 'employee id
    3.         Dim idSA As Integer 'superior id
    4.         Dim numeA As String 'employee name
    5.         Dim functieA As String ' employee job
    6.  
    7.         Dim debugstring As String
    8.         Dim count As IDictionaryEnumerator = AList.GetEnumerator()
    9.         While count.MoveNext()
    10.             If Not (CType(count.Value, TreeNode).Parent Is Nothing) Then
    11.                 If Not count.Key = 0 Then
    12.                     'If count.Key = 0 Then count.MoveNext()
    13.                     idA = count.Key()
    14.                     idSA = IDList(CType(count.Value, TreeNode).Parent)
    15.                     numeA = CType(count.Value, TreeNode).Text
    16.                     functieA = CType(count.Value, TreeNode).Tag.ToString
    17.                     debugstring &= "id: " & idA.ToString & vbTab & "id sup: " &_
    18.  idSA.ToString & vbTab & "nume: " & numeA & vbTab & "functie: " & functieA & vbCrLf
    19.                 End If
    20.                 'introducere in baza de date angajat
    21.                 '...
    22.                 '
    23.             Else : debugstring &= "company node" & vbCrLf 'first node in tree is the company and i _
    24. 'just need to ignore it(this node is added at form load with key 0 in Alist)
    25.             End If
    26.         End While
    27.         MsgBox(debugstring)
    28.         AList.GetEnumerator.Reset()
    29.     End Sub
    Last edited by mindloop; Mar 28th, 2004 at 02:34 PM.
    ehmm...

  10. #10
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    by the way, thanks for the quick reply!
    look, i've cut some code out because it would've just cause confusion, as you notice i'm not a native english speaker, so comments in english i added just for this posts. if you still haven't got the general ideea i'd be happy to send you the whole code. so you can see the whole tree thing.
    ehmm...

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Where are you testing to see that the ID is always 0? When the DragDrop code runs are you checking that oldnodeID's value is not 0?


    Or just post the project.

  12. #12
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    here is the part of the project concerning the treeview. i made it a new project.
    ehmm...

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Ok I don't see whats broken. If I move stuff around and then press Details I get the right ID. What is the trouble? Give me the steps to reproduce the problem.

  14. #14
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    all seems to be allright until you move an entire branch, that is if the node you move has childnodes. the child nodes all receive keys 0. wich i can see why it happens. when moving a branch the code actually clones the branch adds the clone to the new parent and deletes the branch that was being cloned. the problem is that at this step i only refresh the hastables with he new refferences for the node being moved, and leave it's children unhandled.
    to reproduce add two employees to the root node
    then add an employee for each of the previous added nodes. then drag one of the first created employees to the other employe created first.
    ehmm...

  15. #15
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    "details" shows the right id but when pressing "aplica" which means apply (this button should later on save the employee tree to the database)
    aplica will show you the right superiorid until you drag an entire branch.
    what i don't know hov to acomplish is to handle all of the child nodes, or get the superiorID without using the IDlist hastable. it seems to me the alist hastable stores the right key-value pairs after cloning and IDlist does not.
    ehmm...

  16. #16
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    ok done,

    i pulled myself together, and instead asking questions i came with a solution
    i made a recursive procedure to replace all the refferences to the dragged node itself and it's children with the new ones . so now all works fine.

    thanks anyway for the support Edneeis.
    Last edited by mindloop; Sep 11th, 2004 at 04:06 AM.
    ehmm...

  17. #17
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    thx edneeis.
    maybe you should put your example in the codebank.

  18. #18
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You know for some reason I've never added anything to the codebank. I don't even know what you'd title this example.
    Last edited by Edneeis; Sep 8th, 2004 at 09:32 PM.

  19. #19
    New Member
    Join Date
    Sep 2004
    Location
    Brisbane, Australia
    Posts
    3
    I've attached a sample project that illustrates how to add any number of properties to a treenode.

    Hope this helps...
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width