option Explicit
Private mvarSelectKey As String
Private Sub Form_Load()
TreeView1.Indentation = 300 '设置缩进
TreeView1.LineStyle = tvwRootLines '设置显示方式
TreeView1.LabelEdit = tvwManual '不可编辑
TreeView1.Nodes.Add , , "A_aoDi", "A_奥迪" '根节点
TreeView1.Nodes.Add "A_aoDi", tvwChild, "A4L", "A4L" '子节点
TreeView1.Nodes.Add , , "B_baoMa", "B_宝马" '另一个根节点
TreeView1.Nodes.Add "B_baoMa", tvwChild, "B232", "BMW232" ''''
TreeView1.Nodes.Add "B_baoMa", tvwChild, "B525", "BMW525" '''''
Dim msNode As MSComctlLib.Node ''''
For Each msNode In TreeView1.Nodes '展开
msNode.Expanded = True
Next
End Sub
'拖动结束事件
Private Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
Dim strHitKey As String
Dim strName As String
strHitKey = TreeView1.HitTest(x, y).Key
strName = TreeView1.Nodes(mvarSelectKey).Text
TreeView1.Nodes.Remove (mvarSelectKey)
TreeView1.Nodes.Add strHitKey, tvwChild, mvarSelectKey, strName
End Sub
'拖动过程事件
Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
End Sub
'鼠标按下事件
Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim strKey As String
If Button = vbLeftButton Then
If Not TreeView1.SelectedItem Is Nothing Then
strKey = TreeView1.SelectedItem.Key
If TreeView1.Nodes(strKey).Children = 0 Then
mvarSelectKey = strKey
TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage
TreeView1.Drag vbBeginDrag
End If
End If
End If
End Sub
'鼠标移动事件
Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim strKey As String
If Not TreeView1.HitTest(x, y) Is Nothing Then
TreeView1.SelectedItem = TreeView1.HitTest(x, y)
End If
End Sub
Treeview拖曳
最新推荐文章于 2019-04-04 10:16:14 发布