假设:
treeview初始时有一个根节点
根节点下有6个子节点
根节点第2、3、4、5节点各有2个子节点问如果将根节点第3节点和他的下一个兄弟节点(即第4子节点)位置互换
其各自子节点跟着他们原来的父节点走请问如何实现(这个互换只是程序中的一块,但不知如何实现
我本意是读取一些无序的信息入树节点
在读入时根据他们的某些资料赋于其各自的tag 一个属性
然后根据tag属性排序,只要实现某节点和他下一个兄弟节点的位置互换
我就可以通过冒泡排序处理他了)

解决方案 »

  1.   

    Option ExplicitPrivate Enum ObjectType
        otNone = 0
        otFactory = 1
        otGroup = 2
        otPerson = 3
        otFactory2 = 4
        otGroup2 = 5
        otPerson2 = 6
    End EnumPrivate SourceNode As Object
    Private SourceType As ObjectType
    Private TargetNode As Object
    ' ***********************************************
    ' Return the node's object type.
    ' ***********************************************
    Private Function NodeType(test_node As Node) As ObjectType
        If test_node Is Nothing Then
            NodeType = otNone
        Else
            Select Case Left$(test_node.Key, 1)
                Case "f"
                    NodeType = otFactory
                Case "g"
                    NodeType = otGroup
                Case "p"
                    NodeType = otPerson
            End Select
        End If
    End Function
    ' ***********************************************
    ' Prepare the ImageList and TreeView controls.
    ' ***********************************************
    Private Sub Form_Load()
    Dim i As Integer
    Dim factory As Node
    Dim group As Node
    Dim person As Node    ' Load pictures into the ImageList.
        For i = 1 To 6
            TreeImages.ListImages.Add , , TreeImage(i).Picture
        Next i
        
        ' Attach the TreeView to the ImageList.
        OrgTree.ImageList = TreeImages
        
        ' Create some nodes.
        Set factory = OrgTree.Nodes.Add(, , "f R & D", "R & D", otFactory, otFactory2)
        Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Engineering", "Engineering", otGroup, otGroup2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Cameron, Charlie", "Cameron, Charlie", otPerson, otPerson2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Davos, Debbie", "Davos, Debbie", otPerson, otPerson2)
        person.EnsureVisible
        Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Test", "Test", otGroup, otGroup2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Able, Andy", "Andy, Able", otPerson, otPerson2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Baker, Betty", "Baker, Betty", otPerson, otPerson2)
        person.EnsureVisible
        
        Set factory = OrgTree.Nodes.Add(, , "f Sales & Support", "Sales & Support", otFactory, otFactory2)
        Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Showroom Sales", "Showroom Sales", otGroup, otGroup2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Gaines, Gina", "Gaines, Gina", otPerson, otPerson2)
        person.EnsureVisible
        Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Field Service", "Field Service", otGroup, otGroup2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Helms, Harry", "Helms, Harry", otPerson, otPerson2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Ives, Irma", "Ives, Irma", otPerson, otPerson2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Jackson, Josh", "Jackson, Josh", otPerson, otPerson2)
        person.EnsureVisible
        Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Customer Support", "Customer Support", otGroup, otGroup2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Klug, Karl", "Klug, Karl", otPerson, otPerson2)
        Set person = OrgTree.Nodes.Add(group, tvwChild, "p Landau, Linda", "Landau, Linda", otPerson, otPerson2)
        person.EnsureVisible
    End Sub
    ' ***********************************************
    ' Make the TreeView as large as possible.
    ' ***********************************************
    Private Sub Form_Resize()
        OrgTree.Move 0, 0, ScaleWidth, ScaleHeight
    End Sub
    ' Add a new factory.
    Private Sub mnuAddFactory_Click()
    Dim name As String
    Dim factory As Node    name = InputBox("Factory Name", "New Factory", "")
        If name = "" Then Exit Sub
        
        Set factory = OrgTree.Nodes.Add(, , "f " & name, name, otFactory, otFactory2)
        factory.EnsureVisible
    End Sub' Add a new group.
    Private Sub mnuAddGroup_Click()
    Dim name As String
    Dim factory As Node
    Dim group As Node    name = InputBox("Group Name", "New Group", "")
        If name = "" Then Exit Sub
        
        ' Find the factory that should hold the new group.
        Set factory = OrgTree.SelectedItem
        If NodeType(factory) = otPerson Then _
            Set factory = factory.Parent
        If NodeType(factory) = otGroup Then _
            Set factory = factory.Parent    Set group = OrgTree.Nodes.Add(factory, tvwChild, "g " & name, name, otGroup, otGroup2)
        group.EnsureVisible
    End SubPrivate Sub mnuNodes_Click()
    Dim selected_node As Node
    Dim selected_type As ObjectType    Set selected_node = OrgTree.SelectedItem
        If selected_node Is Nothing Then
            selected_type = otNone
        Else
            selected_type = NodeType(selected_node)
        End If    ' You can always add a factory.
        
        ' You can add a group if a factory, person, or
        ' group is selected.
        mnuAddGroup.Enabled = (selected_type <> otNone)
        
        ' You can add a person if a group or person
        ' is selected.
        mnuAddPerson.Enabled = (selected_type = otPerson) _
            Or (selected_type = otGroup)
    End Sub
    ' Add a new person.
    Private Sub mnuAddPerson_Click()
    Dim name As String
    Dim group As Node
    Dim person As Node    name = InputBox("Person Name", "New Person", "")
        If name = "" Then Exit Sub
        
        ' Find the group that should hold the new person.
        Set group = OrgTree.SelectedItem
        If NodeType(group) = otPerson Then _
            Set group = group.Parent    Set person = OrgTree.Nodes.Add(group, tvwChild, "p " & name, name, otPerson, otPerson2)
        person.EnsureVisible
    End Sub' ***********************************************
    ' Save the node pressed so we can drag it later.
    ' ***********************************************
    Private Sub OrgTree_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
        Set SourceNode = OrgTree.HitTest(x, y)
    End Sub
      

  2.   


    ' ***********************************************
    ' Start a drag if one is not in progress.
    ' ***********************************************
    Private Sub OrgTree_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
        If Button = vbLeftButton Then
            ' Start a new drag. Note that we do not get
            ' other MouseMove events while the drag is
            ' in progress.
            
            ' See what node we are dragging.
            SourceType = NodeType(SourceNode)        ' Select this node. When no node is highlighted,
            ' this node will be displayed as selected. That
            ' shows where it will land if dropped.
            Set OrgTree.SelectedItem = SourceNode        ' Set the drag icon for this source.
            OrgTree.DragIcon = IconImage(SourceType)
            OrgTree.Drag vbBeginDrag
        End If
    End Sub
    ' ***********************************************
    ' The user is dropping. See if the drop is valid.
    ' ***********************************************
    Private Sub OrgTree_DragDrop(Source As Control, x As Single, y As Single)
        If Not (OrgTree.DropHighlight Is Nothing) Then
            ' It's a valid drop. Set source node's
            ' parent to be the target node.
            Set SourceNode.Parent = OrgTree.DropHighlight
            Set OrgTree.DropHighlight = Nothing
        End If    Set SourceNode = Nothing
        SourceType = otNone
    End Sub
    ' ***********************************************
    ' The mouse is being dragged over the control.
    ' Highlight the appropriate node.
    ' ***********************************************
    Private Sub OrgTree_DragOver(Source As Control, x As Single, y As Single, State As Integer)
    Dim target As Node
    Dim highlight As Boolean    ' See what node we're above.
        Set target = OrgTree.HitTest(x, y)
        
        ' If it's the same as last time, do nothing.
        If target Is TargetNode Then Exit Sub
        Set TargetNode = target
        
        highlight = False
        If Not (TargetNode Is Nothing) Then
            ' See what kind of node were above.
            If NodeType(TargetNode) + 1 = SourceType Then _
                highlight = True
        End If
        
        If highlight Then
            Set OrgTree.DropHighlight = TargetNode
        Else
            Set OrgTree.DropHighlight = Nothing
        End If
    End Sub
      

  3.   

    拉動主要在DragOver。DragDrop這兩個事件中進行控制,上面的程序可以參考,不連接數據庫的。
      

  4.   

    莫依JJ,我不是要做拖动效果呀
    我只是要根据tag属性对整个树重新排序
      

  5.   

    我只是要根据tag属性对整个树重新排序
    /////
    你是不是在數據庫裡面呀,我不是很懂得你問的意思呀。
    講清楚點點還有,你回別人的代碼都是錯誤的,看來你對treeview 不是很熟悉哦。
      

  6.   

    你設置相通的節點,也就是key了以後就會自動排序列呀。 tag本身隻是一個存儲的東西,好象應該這麼講,但是並不代表什麼的,所以ms是不提倡程序員亂用tag這個屬性的。所以,你可以設置文件夾有相通的key(關鍵字,就可以自動進行排列)
      

  7.   

    你留給mail,我給你發個例子,你去參考下
      

  8.   

    要啊要啊~呵呵!
    可以懒的事情就懒... 麻烦 莫依JJ,飞飞飞GG了[email protected]
      

  9.   

    呵呵,谢谢大家了,昨晚上回家自己琢磨了好久才弄出来,呵
    现将代码附在下面供和我一样的菜花同志们学习,下午三点以后结贴
    (早上忙着上班写程序没时间上来,感谢莫依JJ和飞飞GG)附:解决代码:
    '   对指定节点的各子节点递归调用 SubNodeSort 子程序
    Private Sub SubNodeRecursive(theNode As node)
    Dim i As Integer
    Dim Nc As node    If theNode.Children = 0 Then
            Exit Sub
        ElseIf theNode.Children = 1 Then
            Call SubNodeRecursive(theNode.Child.FirstSibling)
            Exit Sub
        Else
            Set Nc = theNode.Child.FirstSibling
            For i = 1 To theNode.Children
                Call SubNodeSort(Nc)
                Call SubNodeRecursive(Nc)
                If i <> theNode.Children Then Set Nc = Nc.Next
            Next i
            Call SubNodeSort(theNode)
        End If
        
    End Sub'   对指定节点的第一层子节点进行排序
    Private Sub SubNodeSort(theNode As node)
    Dim Nz() As node
    Dim Nc As node, nc2 As node
    Dim i As Integer, j As Integer
        If theNode.Children < 2 Then       '   无子节点
            Exit Sub
        End If
        
        Set Nc = theNode.Child.LastSibling
        For i = 0 To theNode.Children - 1
            ReDim Preserve Nz(i)
            Set Nz(i) = Nc
            Set Nc = Nc.Previous
        Next i
        
        For i = LBound(Nz) To UBound(Nz)
            For j = LBound(Nz) To i
                If Nz(i).Tag > Nz(j).Tag Then
                    Set Nc = Nz(i)
                    Set Nz(i) = Nz(j)
                    Set Nz(j) = Nc
                End If
            Next j
        Next i
        
        For i = LBound(Nz) To UBound(Nz)
            Set Nz(i).parent = Nz(i).parent
        Next i
        
        theNode.Expanded = False        '   关闭节点的展开模式
        
    End Sub
      

  10.   

    对了,该排序程序排序完后关闭了被排序节点的展开
    所以要展开的话别忘了自己加一句如对根节点排序我是用:
        If TreeView1.Nodes.Count > 0 Then        '判断 treeview 控件中是否有节点
            Call SubNodeRecursive(TreeView1.Nodes(1))     '   对根节点排序
            TreeView1.Nodes(1).Expanded = True            '   展开根节点
            TreeView1.SelectedItem = TreeView1.Nodes(1)   '   把根节点置为选中状态
        End If
      

  11.   

    利用sorted属性。
    Private Sub TreeView1_NodeClick(ByVal Node As Node)
       Node.Parent.Sorted = True
    End Sub