Help with a Treeview problem (moving items to the end)
I have gotten some really good help on this issue, from this forum, especially from Doogle..
Let me start by saying thank you very much.
To get this setup.
Create a new Project
1)Add 'Microsoft Windows Common Controls'
2)Add two Treeview controls named:
--tv
--tv1
3)Add three command buttons named:
--cmdLoadTree
--cmdShuffle
--cmdSort
4)Place the attached file ASCII_TREE.TXT in the application directory
5)Copy and paste the following code into the main project window (You will need to delete the default form_load, since it is include in the code below)
6)Run the project
7)Select 'Load Tree'
8)Select 'Sort'
9)Look at Tv1 this is where the information is sorted and redisplayed.
10)Everything NODE ending with an "*" should be sorted to the bottom of the tree, ALONG WITH everything under it.
This is where my problem comes in. The second level is sorted to the end, but not the values underneath it.
I really hope someone can help me with this.
Again Doogle has been very helpful, but I have been unable to contact him to get help with this last issue in the code.
Doogle: if you are out there and can help, it would be much appreciated.
Anyone else, if you can help it would also be much appreciated.
I am really stuck and can't figure out how to move forward.
Thanks
kp
Print screen of tree before selecting SORT
Print screen after selecting SORT
Code:
Option Explicit
Private Declare Function CoCreateGuid Lib "ole32.dll" (pGUID As Any) As Long
Public Function CreateGUID() As String
Dim i As Long, b(0 To 15) As Byte
If CoCreateGuid(b(0)) = 0 Then
For i = 0 To 15
CreateGUID = CreateGUID & Right$("00" & Hex$(b(i)), 2)
Next i
Else
MsgBox "Error While creating GUID!"
End If
End Function
Private Function SetSecondElement(ByRef strS() As String) As Integer
Dim intI As Integer
intI = 1
If UBound(strS) > 0 Then
Do Until strS(intI) <> vbNullString
intI = intI + 1
Loop
strS(1) = strS(intI)
End If
SetSecondElement = UBound(strS)
End Function
Private Sub Shuffle(strData() As String)
'
'Expects an array of values such as :
'AA 111111
'AB 222222
'AC 333333
'AD 444444
' and randomly 'shuffles' the numeric part
' to give, for example
'AA 333333
'AB 222222
'AC 444444
'AD 111111
'
' When complete, strData will contain the 'shuffled' data
' (Assumes there's a 'Randomize' statement in the Form Load event)
'
Dim strResult() As String
Dim strTemp As String
Dim intI As Integer
Dim intJ As Integer
Dim intOL As Integer
Dim intOL1 As Integer
Dim intRnd As Integer
Dim strS() As String
Dim strS1() As String
For intI = 0 To UBound(strData)
'
' Create a Random number within the bounds of the Array
'
intRnd = Int(Rnd * (UBound(strData) + 1))
'
' Form 2 parts of the current element's original data
' strS(0) = letters part
' rstS(1) = numeric part
'
'Debug.Print strData(intI)
'Debug.Print strData(intRnd)
strS = Split(strData(intI), " ")
intOL = SetSecondElement(strS)
'
' Save this element's numeric part
'
strTemp = strS(1)
'
' Create a pair of parts for the Random element
'
strS1 = Split(strData(intRnd), " ")
intOL1 = SetSecondElement(strS1)
'
' Swap the numeric values
'
strS(1) = strS1(1)
strS1(1) = strTemp
'
' Put the modified values back into the original array
'
For intJ = 2 To UBound(strS)
strS(intJ) = vbNullString
Next intJ
For intJ = 2 To UBound(strS1)
strS1(intJ) = vbNullString
Next intJ
strData(intI) = strS(0) & Space(intOL) & strS(1)
strData(intRnd) = strS1(0) & Space(intOL1) & strS1(1)
Next intI
End Sub
Private Function GetLevel(strData As String) As Integer
Dim intI As Integer
intI = 1
Do Until Mid$(strData, intI, 1) <> vbTab
intI = intI + 1
Loop
GetLevel = intI - 1
End Function
Private Sub cmdLoadTree_Click()
LoadNodesFromFileWithTab
End Sub
Private Sub cmdShuffle_Click()
Call ProcessAllChildren(tv.Nodes(1), tv.Nodes(1))
Call ShuffleChildren(tv.Nodes(1))
End Sub
Private Sub cmdSort_Click()
Call ProcessAllNodes(tv.Nodes(1), tv.Nodes(1))
Call SortChildren(tv.Nodes(1))
End Sub
Private Sub Form_Load()
Randomize
End Sub
Public Sub LoadNodesFromFileWithTab()
Dim text_line As String
Dim level As Integer
Dim tree_nodes() As Node
Dim num_nodes As Integer
Dim iFreefile As Integer
Dim Treefile As String
iFreefile = FreeFile
Treefile = "C:\mydir\ASCII_TREE.txt"
Open Treefile For Input As iFreefile
Form1.tv.Nodes.Clear
Form1.Tv1.Nodes.Clear
Do While Not EOF(iFreefile)
Line Input #iFreefile, text_line
level = 1
Do While Left$(text_line, 1) = vbTab
level = level + 1
text_line = Mid$(text_line, 2)
Loop
If level > num_nodes Then
num_nodes = level
ReDim Preserve tree_nodes(1 To num_nodes)
End If
If level = 1 Then
Set tree_nodes(level) = Form1.tv.Nodes.Add(, , CreateGUID, text_line)
Else
Set tree_nodes(level) = Form1.tv.Nodes.Add(tree_nodes(level - 1), tvwChild, CreateGUID, text_line)
If level = 2 Then ' Show level 2
tree_nodes(level).EnsureVisible
End If
End If
Loop
Close iFreefile
Open Treefile For Input As iFreefile
Form1.Tv1.Nodes.Clear
Do While Not EOF(iFreefile)
Line Input #iFreefile, text_line
level = 1
Do While Left$(text_line, 1) = vbTab
level = level + 1
text_line = Mid$(text_line, 2)
Loop
If level > num_nodes Then
num_nodes = level
ReDim Preserve tree_nodes(1 To num_nodes)
End If
If level = 1 Then
Set tree_nodes(level) = Form1.Tv1.Nodes.Add(, , CreateGUID, text_line)
Else
Set tree_nodes(level) = Form1.Tv1.Nodes.Add(tree_nodes(level - 1), tvwChild, CreateGUID, text_line)
If level = 2 Then ' Show level 2
tree_nodes(level).EnsureVisible
End If
End If
Loop
' Form1.tv.Nodes.Item(1).EnsureVisible
Close iFreefile
End Sub
Private Sub ProcessAllChildren(ThisNode As Node, PreviousNode As Node)
Dim NextNode As Node
Dim intI As Integer
Set NextNode = ThisNode.Child
For intI = 1 To ThisNode.Children
If NextNode.Children > 0 Then
Call ProcessAllChildren(NextNode, ThisNode)
Call ShuffleChildren(NextNode)
Set NextNode = NextNode.Next
End If
Next intI
End Sub
Private Sub ShuffleChildren(TheNode As Node)
Dim ch As Node
Dim strChildList() As String
Dim intI As Integer
Dim intChildren As Integer
intChildren = TheNode.Children
If intChildren > 0 Then
ReDim strChildList(intChildren - 1)
Set ch = TheNode.Child
strChildList(0) = ch.Text
For intI = 1 To intChildren - 1
Set ch = ch.Next
strChildList(intI) = ch.Text
Next intI
Shuffle strChildList
Set ch = TheNode.Child
For intI = 0 To UBound(strChildList)
ch.Text = strChildList(intI)
Set ch = ch.Next
Next intI
End If
End Sub
Private Sub ProcessAllNodes(ThisNode As Node, PreviousNode As Node)
Dim NextNode As Node
Dim intI As Integer
Set NextNode = ThisNode.Child
For intI = 1 To ThisNode.Children
If NextNode.Children > 0 Then
Call ProcessAllNodes(NextNode, ThisNode)
Call SortChildren(NextNode)
Set NextNode = NextNode.Next
End If
Next intI
End Sub
Private Sub SortChildren(TheNode As Node)
Dim ch As Node
Dim strChildList() As String
Dim intI As Integer
Dim intChildren As Integer
intChildren = TheNode.Children
If intChildren > 0 Then
ReDim strChildList(intChildren - 1)
Set ch = TheNode.Child
strChildList(0) = ch.Text
For intI = 1 To intChildren - 1
Set ch = ch.Next
strChildList(intI) = ch.Text
Next intI
SortChild strChildList
Set ch = TheNode.Child
For intI = 0 To UBound(strChildList)
ch.Text = strChildList(intI)
Set ch = ch.Next
Next intI
End If
End Sub
Private Sub SortChild(strData() As String)
Dim strResult() As String
Dim strTemp As String
Dim boSwap As Boolean
Dim intI As Integer
Dim intJ As Integer
Dim intOL As Integer
Dim intOL1 As Integer
Dim intRnd As Integer
Dim strS() As String
Dim strS1() As String
For intI = 0 To UBound(strData) - 1
boSwap = False
If Mid$(strData(intI), Len(strData(intI)), 1) = "*" Then
intJ = intI + 1
Do
If Mid$(strData(intJ), Len(strData(intJ)), 1) <> "*" Then
intRnd = intJ
strS = Split(strData(intI), " ")
intOL = SetSecondElement(strS)
strTemp = strS(1)
strS1 = Split(strData(intRnd), " ")
intOL1 = SetSecondElement(strS1)
strS(1) = strS1(1)
strS1(1) = strTemp
For intJ = 2 To UBound(strS)
strS(intJ) = vbNullString
Next intJ
For intJ = 2 To UBound(strS1)
strS1(intJ) = vbNullString
Next intJ
strData(intI) = strS(0) & Space(intOL) & strS(1)
strData(intRnd) = strS1(0) & Space(intOL1) & strS1(1)
boSwap = True
Else
intJ = intJ + 1
End If
Loop Until intJ > UBound(strData) Or boSwap = True
End If
Next intI
End Sub
Re: Help with a Treeview problem (moving items to the end)
It's not a particularly 'easy' problem to solve. I'll take another look at the weekend. At the moment there's nothing 'obvious' I can se that's causing the issue
Re: Help with a Treeview problem (moving items to the end)
Your problem is that you are sorting the names of the nodes and not the nodes themselves. When you swap things within SortChild(), you are merely swapping the names. Instead, you need either swap the nodes directly or swap all the properties of the nodes. I suspect it would be much easier to just swap the nodes directly.
Re: Help with a Treeview problem (moving items to the end)
Hello again:
I have been struggling with this issue for over a month and I think we are close, but wanted to make a plea to people on this
forum for help.
I don't know if it is ok to offer payment for help, but I would be willing to pay for an hour of work to get this problem resolved
Thank you in advance for your support and attention to my problem.
Several members have been very helpful and have gotten me much further than I ever thought I would.
Someone stated that the code was simply moving the text, and not the node (node properties).
I need to be able to sort any node that ends with an "*", and all the nodes under the given node to the bottom as well.
Right now it appears to be sorting the first level, but the levels below are not sorted (they don't stay with their parent).
A twist to this is the left part of the node text, needs to remain where it is, and the right part (containing the numerica values and "*")
need to be moved.
For example:
AC 6959*
The program needs to move 6959* to the bottom of the tree, but leave AC in place.
Here is an example:
A
AA 3234
AA1 7865
AA2 6597
AB 0976
AB1 8790
AB2 5476
AC 6959*
AC1 6923*
AC2 5467
AD 9834
AD1 6578*
AD2 6545
This would sort to: (DESIRED STATE)
A
AA 3234
AA1 7865
AA2 6597
AB 0976
AB1 8790
AB2 5476
AC 9834
AC1 6545
AC2 6578*
AD 6959*
AD1 5467
AD1 6923*
The value 6959 is moved, but not values under it do not move.
Right now it does not 'move' the nodes under the node being moved. So I am getting the following results.
6923, 5467, 6578 and 6545 are not moved. I am under the impression if the program moves the NODE and not just
the text values, the values underneath would move as needed.
A
A
AA 3234
AA1 7865
AA2 6597
AB 0976
AB1 8790
AB2 5476
AC 9834
AC1 5467
AC2 6923*
AD 6959*
AD1 6545
AD2 6578*
AC 6959* is moved down, along with the values under that node. So, AC1 6923* and AC2 5497. Remember the left portion numbers
remain. So this becomes AD 6959*, AD2 5497 and AD1 6923*.. NOTE: 6923* is moved down as well. Since it is the last level, the
program would just move all of them with "*" to the end.
Here is the code, I believe that is trying to move the node and its properties. It can be found in SortChild
NOTE: The last version of the entire code can be found in item #55 of this thread.
Code:
Set chTemp = ch1(intI)
Set ch1(intI) = ch1(intRnd)
Set ch1(intRnd) = chTemp