Results 1 to 10 of 10

Thread: Help with a Treeview problem (moving items to the end)

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    69

    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
    Name:  BEFORE.png
Views: 197
Size:  12.7 KB
    Print screen after selecting SORT
    Name:  AFTER.png
Views: 188
Size:  13.8 KB

    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
    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