• OPPO Watch Free inceleme!

    AMOLED ekranlı ve nispeten daha uygun fiyatlı akıllı saat OPPO Watch Free modelini inceledik. İşte özellikleri!

VB.NET Treeview'de sürükle bırak metodu ile listede sıralamaları değiştirme nasıl?

Bu konuyu okuyanlar

Vatansever

Asistan
Katılım
23 Ağustos 2007
Mesajlar
405
Reaksiyon puanı
1
Puanları
18
Arkadaşlar Treeview'de seçenekler ekliyorum ve bu seçeneklerin sıralamalarını değiştirmek istiyorum altta bir kod buldum bunu denedim bu kod ile bir seçeneği seçip sürüklüyorum başka birinin üzerine bırakıyorum ve o seçenek bıraktığımın içinde ayrı bir dal oluşturuyor, ayrı bir dal oluşturmasın sadece sıralama değişsin istiyorum kodu düzeltebilirmisiniz?

http://www.codeproject.com/Articles/8995/Introduction-to-TreeView-Drag-and-Drop-VB-NET

treeView'im şöyle
Kod:
+Liste
     +değer1
     +değer2
     +değer3
     +değer4
     +değer5
     +değer6
     +değer7
     +değer8

Kod:
    Public Sub TreeView1_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag, TreeView2.ItemDrag

        'Set the drag node and initiate the DragDrop
        DoDragDrop(e.Item, DragDropEffects.Move)

    End Sub

    Public Sub TreeView1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragEnter, TreeView2.DragEnter

        'See if there is a TreeNode being dragged
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then
            'TreeNode found allow move effect
            e.Effect = DragDropEffects.Move
        Else
            'No TreeNode found, prevent move
            e.Effect = DragDropEffects.None
        End If

    End Sub

    Public Sub TreeView1_DragOver(ByVal sender As System.Object, ByVal e As DragEventArgs) Handles TreeView1.DragOver, TreeView2.DragOver

        'Check that there is a TreeNode being dragged
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) = False Then Exit Sub

        'Get the TreeView raising the event (incase multiple on form)
        Dim selectedTreeview As TreeView = CType(sender, TreeView)

        'As the mouse moves over nodes, provide feedback to the user
        'by highlighting the node that is the current drop target
        Dim pt As Point = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
        Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt)

        'See if the targetNode is currently selected, if so no need to validate again
        If Not (selectedTreeview Is targetNode) Then
            'Select the node currently under the cursor
            selectedTreeview.SelectedNode = targetNode

            'Check that the selected node is not the dropNode and also that it
            'is not a child of the dropNode and therefore an invalid target
            Dim dropNode As TreeNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
            Do Until targetNode Is Nothing
                If targetNode Is dropNode Then 
                    e.Effect = DragDropEffects.None
                    Exit Sub
                End If
                targetNode = targetNode.Parent
            Loop
        End If

        'Currently selected node is a suitable target, allow the move
        e.Effect = DragDropEffects.Move

    End Sub

    Public Sub TreeView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragDrop, TreeView2.DragDrop

        'Check that there is a TreeNode being dragged
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) = False Then Exit Sub

        'Get the TreeView raising the event (incase multiple on form)
        Dim selectedTreeview As TreeView = CType(sender, TreeView)

        'Get the TreeNode being dragged
        Dim dropNode As TreeNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)

        'The target node should be selected from the DragOver event
        Dim targetNode As TreeNode = selectedTreeview.SelectedNode

        'Remove the drop node from its current location
        dropNode.Remove()

        'If there is no targetNode add dropNode to the bottom of the TreeView root
        'nodes, otherwise add it to the end of the dropNode child nodes
        If targetNode Is Nothing Then
            selectedTreeview.Nodes.Add(dropNode)
        Else
            targetNode.Nodes.Add(dropNode)
        End If

        'Ensure the newley created node is visible to the user and select it
        dropNode.EnsureVisible()
        selectedTreeview.SelectedNode = dropNode

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Allow the TreeViews to be the target of a DragDrop operation
        'and populate it with some random TreeNodes for the example
        TreeView1.AllowDrop = True
        TreeView1.HideSelection = False

    End Sub
 

Vatansever

Asistan
Katılım
23 Ağustos 2007
Mesajlar
405
Reaksiyon puanı
1
Puanları
18
Arkadaşlar isteğime daha uygun bir kod buldum iç içe dallanma yapmıyor ama sorun şu sadece ana dizinleri kaydırıyor(sıralamasını değiştiriyor) yani benim projede Liste kısmını mesela liste Liste1 Liste2 Liste3 gibi olsaydı tamamdı ama ben bir alt dalın sıralamasını değiştirmek istiyorum değer1 değer2 değer3 değer4 bunların sırasını,,,, Liste ana dizini bunların dışında tutulacak sabit kalacak. Hangi kod kolayınıza gelirse bir el atabilirmisiniz??

Kod:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler Me.TreeView1.MouseDown, AddressOf Me.treeView1_MouseDown
            AddHandler Me.TreeView1.DragOver, AddressOf Me.treeView1_DragOver
            AddHandler Me.TreeView1.DragEnter, AddressOf Me.treeView1_DragEnter
            AddHandler Me.TreeView1.ItemDrag, AddressOf Me.treeView1_ItemDrag
            AddHandler Me.TreeView1.DragDrop, AddressOf Me.treeView1_DragDrop
        End Sub

        Private Sub treeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Me.TreeView1.SelectedNode = Me.TreeView1.GetNodeAt(e.X, e.Y)
        End Sub
        Private Sub treeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs)
            DoDragDrop(e.Item, DragDropEffects.Move)
        End Sub
        Private Sub treeView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
            e.Effect = DragDropEffects.Move
        End Sub
        Private Sub treeView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
            If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) AndAlso Not (Me.NodeMap = "") Then
                Dim MovingNode As TreeNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
                Dim NodeIndexes As String() = Me.NodeMap.Split("|"c)
                Dim InsertCollection As TreeNodeCollection = Me.TreeView1.Nodes
                Dim i As Integer = 0
                While i < NodeIndexes.Length - 1
                    InsertCollection = InsertCollection(Int32.Parse(NodeIndexes(i))).Nodes
                    System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
                End While
                If Not (InsertCollection Is Nothing) Then
                    InsertCollection.Insert(Int32.Parse(NodeIndexes(NodeIndexes.Length - 1)), CType(MovingNode.Clone, TreeNode))
                    Me.TreeView1.SelectedNode = InsertCollection(Int32.Parse(NodeIndexes(NodeIndexes.Length - 1)))
                    MovingNode.Remove()
                End If
            End If
        End Sub
        Private Sub treeView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
            Dim NodeOver As TreeNode = Me.TreeView1.GetNodeAt(Me.TreeView1.PointToClient(Cursor.Position))
            Dim NodeMoving As TreeNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
            If Not (NodeOver Is Nothing) AndAlso (Not (NodeOver Is NodeMoving) OrElse (Not (NodeOver.Parent Is Nothing) AndAlso NodeOver.Index = (NodeOver.Parent.Nodes.Count - 1))) Then
                Dim OffsetY As Integer = Me.TreeView1.PointToClient(Cursor.Position).Y - NodeOver.Bounds.Top
                Dim NodeOverImageWidth As Integer = Me.TreeView1.ImageList.Images(NodeOver.ImageIndex).Size.Width + 8
                Dim g As Graphics = Me.TreeView1.CreateGraphics
                If NodeOver.ImageIndex = 1 Then
                    If OffsetY < (NodeOver.Bounds.Height / 2) Then
                        Dim tnParadox As TreeNode = NodeOver
                        While Not (tnParadox.Parent Is Nothing)
                            If tnParadox.Parent Is NodeMoving Then
                                Me.NodeMap = ""
                                Return
                            End If
                            tnParadox = tnParadox.Parent
                        End While
                        SetNewNodeMap(NodeOver, False)  ' ERROR!  Not sure why
                        If SetMapsEqual() = True Then
                            Return
                        End If
                        Me.Refresh()
                        Me.DrawLeafTopPlaceholders(NodeOver)
                    Else
                        Dim tnParadox As TreeNode = NodeOver
                        While Not (tnParadox.Parent Is Nothing)
                            If tnParadox.Parent Is NodeMoving Then
                                Me.NodeMap = ""
                                Return
                            End If
                            tnParadox = tnParadox.Parent
                        End While
                        Dim ParentDragDrop As TreeNode = Nothing
                        If Not (NodeOver.Parent Is Nothing) AndAlso NodeOver.Index = (NodeOver.Parent.Nodes.Count - 1) Then
                            Dim XPos As Integer = Me.TreeView1.PointToClient(Cursor.Position).X
                            If XPos < NodeOver.Bounds.Left Then
                                ParentDragDrop = NodeOver.Parent
                                If XPos < (ParentDragDrop.Bounds.Left - Me.TreeView1.ImageList.Images(ParentDragDrop.ImageIndex).Size.Width) Then
                                    If Not (ParentDragDrop.Parent Is Nothing) Then
                                        ParentDragDrop = ParentDragDrop.Parent
                                    End If
                                End If
                            End If
                        End If
                        SetNewNodeMap(Microsoft.VisualBasic.IIf(Not (ParentDragDrop Is Nothing), ParentDragDrop, NodeOver), True)
                        If SetMapsEqual() = True Then
                            Return
                        End If
                        Me.Refresh()
                        DrawLeafBottomPlaceholders(NodeOver, ParentDragDrop)
                    End If
                Else
                    If OffsetY < (NodeOver.Bounds.Height / 3) Then
                        Dim tnParadox As TreeNode = NodeOver
                        While Not (tnParadox.Parent Is Nothing)
                            If tnParadox.Parent Is NodeMoving Then
                                Me.NodeMap = ""
                                Return
                            End If
                            tnParadox = tnParadox.Parent
                        End While
                        SetNewNodeMap(NodeOver, False)
                        If SetMapsEqual() = True Then
                            Return
                        End If
                        Me.Refresh()
                        Me.DrawFolderTopPlaceholders(NodeOver)
                    Else
                        If (Not (NodeOver.Parent Is Nothing) AndAlso NodeOver.Index = 0) AndAlso (OffsetY > (NodeOver.Bounds.Height - (NodeOver.Bounds.Height / 3))) Then
                            Dim tnParadox As TreeNode = NodeOver
                            While Not (tnParadox.Parent Is Nothing)
                                If tnParadox.Parent Is NodeMoving Then
                                    Me.NodeMap = ""
                                    Return
                                End If
                                tnParadox = tnParadox.Parent
                            End While
                            SetNewNodeMap(NodeOver, True)
                            If SetMapsEqual() = True Then
                                Return
                            End If
                            Me.Refresh()
                            DrawFolderTopPlaceholders(NodeOver)
                        Else
                            If NodeOver.Nodes.Count > 0 Then
                                NodeOver.Expand()
                            Else
                                If NodeMoving Is NodeOver Then
                                    Return
                                End If
                                Dim tnParadox As TreeNode = NodeOver
                                While Not (tnParadox.Parent Is Nothing)
                                    If tnParadox.Parent Is NodeMoving Then
                                        Me.NodeMap = ""
                                        Return
                                    End If
                                    tnParadox = tnParadox.Parent
                                End While
                                SetNewNodeMap(NodeOver, False)
                                NewNodeMap = NewNodeMap.Insert(NewNodeMap.Length, "|0")
                                If SetMapsEqual() = True Then
                                    Return
                                End If
                                Me.Refresh()
                                DrawAddToFolderPlaceholder(NodeOver)
                            End If
                        End If
                    End If
                End If
            End If
        End Sub
        Private Sub DrawLeafTopPlaceholders(ByVal NodeOver As TreeNode)
            Dim g As Graphics = Me.TreeView1.CreateGraphics
            Dim NodeOverImageWidth As Integer = Me.TreeView1.ImageList.Images(NodeOver.ImageIndex).Size.Width + 8
            Dim LeftPos As Integer = NodeOver.Bounds.Left - NodeOverImageWidth
            Dim RightPos As Integer = Me.TreeView1.Width - 4
            Dim LeftTriangle As Point() = New Point(4) {New Point(LeftPos, NodeOver.Bounds.Top - 4), New Point(LeftPos, NodeOver.Bounds.Top + 4), New Point(LeftPos + 4, NodeOver.Bounds.Y), New Point(LeftPos + 4, NodeOver.Bounds.Top - 1), New Point(LeftPos, NodeOver.Bounds.Top - 5)}
            Dim RightTriangle As Point() = New Point(4) {New Point(RightPos, NodeOver.Bounds.Top - 4), New Point(RightPos, NodeOver.Bounds.Top + 4), New Point(RightPos - 4, NodeOver.Bounds.Y), New Point(RightPos - 4, NodeOver.Bounds.Top - 1), New Point(RightPos, NodeOver.Bounds.Top - 5)}
            g.FillPolygon(System.Drawing.Brushes.Black, LeftTriangle)
            g.FillPolygon(System.Drawing.Brushes.Black, RightTriangle)
            g.DrawLine(New System.Drawing.Pen(Color.Black, 2), New Point(LeftPos, NodeOver.Bounds.Top), New Point(RightPos, NodeOver.Bounds.Top))
        End Sub
        Private Sub DrawLeafBottomPlaceholders(ByVal NodeOver As TreeNode, ByVal ParentDragDrop As TreeNode)
            Dim g As Graphics = Me.TreeView1.CreateGraphics
            Dim NodeOverImageWidth As Integer = Me.TreeView1.ImageList.Images(NodeOver.ImageIndex).Size.Width + 8
            Dim LeftPos As Integer
            Dim RightPos As Integer
            If Not (ParentDragDrop Is Nothing) Then
                LeftPos = ParentDragDrop.Bounds.Left - (Me.TreeView1.ImageList.Images(ParentDragDrop.ImageIndex).Size.Width + 8)
            Else
                LeftPos = NodeOver.Bounds.Left - NodeOverImageWidth
            End If
            RightPos = Me.TreeView1.Width - 4
            Dim LeftTriangle As Point() = New Point(4) {New Point(LeftPos, NodeOver.Bounds.Bottom - 4), New Point(LeftPos, NodeOver.Bounds.Bottom + 4), New Point(LeftPos + 4, NodeOver.Bounds.Bottom), New Point(LeftPos + 4, NodeOver.Bounds.Bottom - 1), New Point(LeftPos, NodeOver.Bounds.Bottom - 5)}
            Dim RightTriangle As Point() = New Point(4) {New Point(RightPos, NodeOver.Bounds.Bottom - 4), New Point(RightPos, NodeOver.Bounds.Bottom + 4), New Point(RightPos - 4, NodeOver.Bounds.Bottom), New Point(RightPos - 4, NodeOver.Bounds.Bottom - 1), New Point(RightPos, NodeOver.Bounds.Bottom - 5)}
            g.FillPolygon(System.Drawing.Brushes.Black, LeftTriangle)
            g.FillPolygon(System.Drawing.Brushes.Black, RightTriangle)
            g.DrawLine(New System.Drawing.Pen(Color.Black, 2), New Point(LeftPos, NodeOver.Bounds.Bottom), New Point(RightPos, NodeOver.Bounds.Bottom))
        End Sub
        Private Sub DrawFolderTopPlaceholders(ByVal NodeOver As TreeNode)
            Dim g As Graphics = Me.TreeView1.CreateGraphics
            Dim NodeOverImageWidth As Integer = Me.TreeView1.ImageList.Images(NodeOver.ImageIndex).Size.Width + 8
            Dim LeftPos As Integer
            Dim RightPos As Integer
            LeftPos = NodeOver.Bounds.Left - NodeOverImageWidth
            RightPos = Me.TreeView1.Width - 4
            Dim LeftTriangle As Point() = New Point(4) {New Point(LeftPos, NodeOver.Bounds.Top - 4), New Point(LeftPos, NodeOver.Bounds.Top + 4), New Point(LeftPos + 4, NodeOver.Bounds.Y), New Point(LeftPos + 4, NodeOver.Bounds.Top - 1), New Point(LeftPos, NodeOver.Bounds.Top - 5)}
            Dim RightTriangle As Point() = New Point(4) {New Point(RightPos, NodeOver.Bounds.Top - 4), New Point(RightPos, NodeOver.Bounds.Top + 4), New Point(RightPos - 4, NodeOver.Bounds.Y), New Point(RightPos - 4, NodeOver.Bounds.Top - 1), New Point(RightPos, NodeOver.Bounds.Top - 5)}
            g.FillPolygon(System.Drawing.Brushes.Black, LeftTriangle)
            g.FillPolygon(System.Drawing.Brushes.Black, RightTriangle)
            g.DrawLine(New System.Drawing.Pen(Color.Black, 2), New Point(LeftPos, NodeOver.Bounds.Top), New Point(RightPos, NodeOver.Bounds.Top))
        End Sub
        Private Sub DrawAddToFolderPlaceholder(ByVal NodeOver As TreeNode)
            Dim g As Graphics = Me.TreeView1.CreateGraphics
            Dim RightPos As Integer = NodeOver.Bounds.Right + 6
            Dim RightTriangle As Point() = New Point(4) {New Point(RightPos, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2) + 4), New Point(RightPos, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2) + 4), New Point(RightPos - 4, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2)), New Point(RightPos - 4, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2) - 1), New Point(RightPos, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2) - 5)}
            Me.Refresh()
            g.FillPolygon(System.Drawing.Brushes.Black, RightTriangle)
        End Sub
        Private Sub SetNewNodeMap(ByVal tnNode As TreeNode, ByVal boolBelowNode As Boolean)
            NewNodeMap.Length = 0
            If boolBelowNode Then
                NewNodeMap.Insert(0, CType(tnNode.Index, Integer) + 1)
            Else
                NewNodeMap.Insert(0, CType(tnNode.Index, Integer))
            End If
            Dim tnCurNode As TreeNode = tnNode
            While Not (tnCurNode.Parent Is Nothing)
                tnCurNode = tnCurNode.Parent
                If NewNodeMap.Length = 0 AndAlso boolBelowNode = True Then
                    NewNodeMap.Insert(0, (tnCurNode.Index + 1) + "|")
                Else
                    NewNodeMap.Insert(0, tnCurNode.Index + "|")
                End If
            End While
        End Sub
        Private Function SetMapsEqual() As Boolean
            If Me.NewNodeMap.ToString = Me.NodeMap Then
                Return True
            Else
                Me.NodeMap = Me.NewNodeMap.ToString
                Return False
            End If
        End Function
 
Üst