Dim TList As Collection
Private Sub UserForm_Initialize()
With Me
.btnClose.Caption = "Close"
.lblSpec = vbNullString
With .TreeView1
.Style = tvwTreelinesPlusMinusText
.LineStyle = tvwRootLines
.SingleSel = True
.HotTracking = True
End With
End With
Set TList = New Collection
Call loadList
End Sub
'Load the collection with the data
Private Sub loadList()
Dim rngData
Dim tc As String, md As String, mKey As String, mTxt As String
Dim idx As Long
'Gets the distinct values from the column
mods = Globals.getDistinctData
'Get the testcase ids
Set rngData = ActiveWorkbook.Worksheets("Sheet1").Range("DataRng")
With rngData
For idx = 1 To .Cells.count
If Not IsEmpty(.Cells(idx, 1)) Then
' get the id
tc = .Cells(idx, 1)
' get the name
md = rngData.Offset(0, -2).Cells(idx, 1)
' if the tc is not empty
If (Len(tc) > 0) Then
' build the key and the text
mKey = "tc:" & TList.count + 1
mTxt = (TList.count + 1) & ". " & md & " : " & tc
'add to the collection
TList.Add mTxt, mKey
End If
End If
Next idx
'Update the Tree
If (TList.count > 0) Then
Call updateTree(TList)
End If
End With
End Sub
'Select the clicked node
Private Sub Treeview1_NodeClick(ByVal Node As MSComctlLib.Node)
Me.lblSpec.Caption = "Moving Node: " & Node.Index
TreeView1.SelectedItem = Node
End Sub
'Highlight what is dragged over
Private Sub TreeView1_OLEDragOver(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single, State As Integer)
Dim nodSelected As Node
Dim nodOver As Node
If TreeView1.SelectedItem Is Nothing Then
Set nodSelected = TreeView1.HitTest(x * 20, y * 20)
If Not nodSelected Is Nothing Then
nodSelected.Selected = True
End If
Else
If TreeView1.HitTest(x * 20, y * 20) Is Nothing Then
Else
Set nodOver = TreeView1.HitTest(x * 20, y * 20)
Set TreeView1.DropHighlight = nodOver
End If
End If
'Scrolling doesnt work yet
If y > 0 And y < 100 Then
m_iScrollDir = -1
ElseIf y > (TreeView1.Height - 200) And y < TreeView1.Height Then
m_iScrollDir = 1
End If
End Sub
'Move the selected node after the destination node
Private Sub TreeView1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
Dim sNode As Node
Dim dNode As Node
Dim dNodeKey As String
Dim dNodeTxt As String
Dim newNodeKey As String
Dim newNodeTxt As String
On Error Resume Next
Effect = fmDropEffectMove
Set sNode = TreeView1.SelectedItem
Set dNode = TreeView1.HitTest(x * 20, y * 20)
newNodeKey = sNode.Key
newNodeTxt = sNode.Text
Set TreeView1.DropHighlight = dNode
' remove the item from the list
TList.Remove sNode.Key
' add the new item using the [After] param to place the item where
'we want to
TList.Add newNodeTxt, newNodeKey, , dNode.Key
' clear the Tree
TreeView1.Nodes.clear
' update the tree
Call updateTree(TList)
End Sub
'update the tree based the collection order
'since the Collection key and the TreeView key is the same
Private Sub updateTree(mCollection As Collection)
Dim idx As Long
With Me.TreeView1.Nodes
.clear
For idx = 1 To mCollection.count
.Add Key:="tc:" & idx, Text:=mCollection.item(idx)
Next idx
End With
Stop
End Sub
Private Sub btnClose_Click()
Unload Me
End Sub