Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21251

Moving Values in Treeview

$
0
0
All:
I have tried and tried and can't seem to figure this out. I thought I would post another note to see if anyone can provide some direction.

What I have is a treeview, when the user selects 'Shuffle', all the values need to be scrambled, but they have to stay with their parent.

Actually if someone could just help me scramble the values, regardless of the left side, I can put the treeview back together.

Folks have been very help in getting me to this point, but right now I am just lost..

I have included the code and the .TXT file you can use to see the problem.

I am hoping and keeping my fingers crossed someone can help me out.

Attachment 97751


Code:

Option Explicit
Private Declare Function CoCreateGuid Lib "ole32.dll" (pGUID As Any) As Long

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 Sub Cmd_Shuffle_Click()
Call ProcessAllChildren(tv.Nodes(1), tv.Nodes(1))
Call ShuffleChildren(tv.Nodes(1))
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

'=================================================
'Shouldn't need to bother with the code below
'=================================================

Private Sub Form_Load()
Randomize
LoadNodesFromFileWithTab
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 = "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

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

Attached Images
 
Attached Files

Viewing all articles
Browse latest Browse all 21251

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>