|
-
Dec 10th, 2003, 02:45 PM
#1
Thread Starter
Addicted Member
Newbie Treeview Remove Nodes Question
Last edited by pinkclown; Jan 5th, 2004 at 01:55 PM.
-
Dec 10th, 2003, 03:52 PM
#2
The parameter to the Remove method is either the Index(numeric) or Key(string) of the Node. The y variable is an array of nodes. First, you need to indicate which element of the array, ie which node, you want to remove and then specify either the Index or Key of that Node. Sorry for the bad explanation but I hope you understand.
VB Code:
Dim i As Long
For i = 0 To UBound(y) - 1
Tree1.Nodes.Remove y(i).key 'or y(i).Index
Next
-
Jan 5th, 2004, 01:55 PM
#3
Thread Starter
Addicted Member
Hmm now working on this project again I just noticed something else
It is working great except it removes all children from every node. In other words, with the nodes that have children, it does display the node, but it also goes in and removes its children because they themselves don't have children. Is there any way to specify that it only remove a node if the "top level" doesn't have children, and then don't bother checking its children's children?
Here's a repost of the code
Code:
Dim x As Node, y() As Node: ReDim y(0)
For Each x In tvwTree.Nodes
If x.Child Is Nothing Then
Set y(UBound(y)) = x
ReDim Preserve y(UBound(y) + 1)
End If
Next
Dim i As Long
For i = 0 To UBound(y) - 1
tvwTree.Nodes.Remove y(i).Index
Next
Thanks in advance
-
Jan 5th, 2004, 02:13 PM
#4
VB Code:
Dim x As Node, y() As Node: ReDim y(0)
For Each x In tvwTree.Nodes
If x.Parent.Index = 1 Then ' Only nodes that are direct children of the root qualify
If x.Child Is Nothing Then
Set y(UBound(y)) = x
ReDim Preserve y(UBound(y) + 1)
End If
End If
Next
-
Jan 5th, 2004, 05:54 PM
#5
Thread Starter
Addicted Member
Thanks Martin, I'm sure it would work but I think I don't have my parent/child set properly. It keeps giving an "object variable or with block variable not set" on the "If x.Parent.Index = 1 Then" line.
What the program is supposed to be doing is making a tree of multiple folders that each have shortcuts in them (the folder names are the nodes, the shortcuts are the children). This is how I have the node creation lines set up... anyone know what am I doing wrong I know it's gotta be something easy...
Code:
Set nParent = tvwTree.Nodes.Add(, , strFolderName, strFolderName)
Set nNode = tvwTree.Nodes.Add(nParent, tvwChild, , strLinkName)
Thanks
-
Jan 5th, 2004, 08:59 PM
#6
No, it's my fault.
VB Code:
For Each x In tvwDemo.Nodes
If x.Index <> 1 Then
If x.Parent.Index = 1 Then ' Only nodes that are direct children of the root qualify
If x.Child Is Nothing Then
Set y(UBound(y)) = x
ReDim Preserve y(UBound(y) + 1)
End If
End If
End If
Next
-
Jan 6th, 2004, 01:39 PM
#7
Thread Starter
Addicted Member
Thanks Martin, but it's still giving the "object variable or with block variable not set" error
-
Jan 6th, 2004, 01:43 PM
#8
Did you change tvwDemo.Nodes to tvwTree.Nodes when you used my code? If you did then please attach your project.
-
Jan 8th, 2004, 12:59 PM
#9
Thread Starter
Addicted Member
I did.. here is the code. I left some of the other things I've tried in there, just commented.
Code:
Private Sub MakeNodes()
Dim strFolderName As String
Dim strShortcutLoc As String
Dim arrFolderSplitter() As String
Dim arrShortcutSplitter() As String
Dim fFile As Integer
Dim strFileName As String
Dim intCount As Integer
Dim intArrayCount As Long
fFile = FreeFile()
Set fso = New FileSystemObject
Set fsfolder = fso.GetFolder(strAppFolderPath)
intCount = 0
For Each fsfoldername In fsfolder.SubFolders
intCount = intCount + 1
arrFolderSplitter = Split(fsfoldername, "\")
strFolderName = arrFolderSplitter(UBound(arrFolderSplitter))
If IsNumeric(strFolderName) Then
MsgBox "oops"
Else
Dim n As Node
' Set n = tvwTree.Nodes.Add(, , , strFolderName)
Set nodParent = tvwTree.Nodes.Add(, , strFolderName, strFolderName)
End If
Set fsShortcutFolder = fso.GetFolder(fsfolder & "\" & strFolderName)
For Each fsfile In fsShortcutFolder.Files
intArrayCount = intArrayCount + 1
arrFileName = Split(fsfile, "\")
strFileName = arrFileName(UBound(arrFileName))
If Right(strFileName, 4) = ".lnk" Then
strFileName = Left(strFileName, (Len(strFileName) - 4))
' tvwTree.Nodes.Add n, , strFileName, strFileName
tvwTree.Nodes.Add strFolderName, tvwChild, , strFileName
' Set nodNode = tvwTree.Nodes.Add(nodParent, tvwChild, , strFileName)
End If
ReDim Preserve arrFileList(intArrayCount)
arrFileList(intArrayCount) = fsfile
Next
Next
Dim x As Node, y() As Node: ReDim y(0)
For Each x In tvwTree.Nodes
If x.Child Is Nothing Then
If x.Index <> 1 Then
If x.Parent.Index = 1 Then
Set y(UBound(y)) = x
ReDim Preserve y(UBound(y) + 1)
End If
End If
End If
Next
Dim i As Long
For i = 0 To UBound(y) - 1
tvwTree.Nodes.Remove y(i).Index
Next
End Sub
Thanks
-
Jan 9th, 2004, 12:19 PM
#10
Thread Starter
Addicted Member
-
Jan 9th, 2004, 03:13 PM
#11
A top level node does not have a Parent (all nodes do have a Root) object. Check if the Node's Parent is nothing before checking its Index.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|