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.
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:
Friend Function GetNodeIndex(ByRef ndc As TreeNodeCollection, ByVal iIndex As Integer) As TreeNode
Dim nd As TreeNode
For Each nd In ndc
If nd.Tag = iIndex Then Return nd
Dim snd As TreeNode = GetNodeIndex(nd.Nodes, iIndex)
If Not IsNothing(snd) Then Return snd
Next
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 )
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.
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:
Friend AList As New Hashtable()
Friend IDList As New Hashtable()
private sub addnodes(byval name as string, byval employeeID as integer, byval superiorId as integer , byval job as string)
Dim newNode As New TreeNode(name)
newNode.Tag = job
newNode.ImageIndex = 1
newNode.SelectedImageIndex = 1
AList.Add(employeeID, newNode)
IDList.Add(newNode, employeeID)
' i need the two hastables so i can 1) retrieve a node by id and 2) get the id of the node via selectednode
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.
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?
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:
Private Sub readtree()
Dim idA As Integer 'employee id
Dim idSA As Integer 'superior id
Dim numeA As String 'employee name
Dim functieA As String ' employee job
Dim debugstring As String
Dim count As IDictionaryEnumerator = AList.GetEnumerator()
While count.MoveNext()
If Not (CType(count.Value, TreeNode).Parent Is Nothing) Then
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.
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.
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.
"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.
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.