微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

VB.Net程序设计:从DataGridView拖放一行数据到TreeView中的某个节点

拖放操作:从DataGridView拖放一行数据到TreeView中的某个节点。

拖动过程中会高亮鼠标所在的节点。

拖出时候会恢复节点正常样式。


    Dim dropNode As TreeNode

    Private Sub Dv_MouseMove(sender As System.Object,e As System.Windows.Forms.MouseEventArgs) Handles Dv.MouseMove
        If (e.Button And Windows.Forms.MouseButtons.Left) = Windows.Forms.MouseButtons.Left Then
            Dim dvs As DataGridView = CType(sender,DataGridView)
            If dvs.HitTest(e.X,e.Y).Type = DataGridViewHitTestType.Cell Then
                If dvs.SelectedRows.Count > 0 Then
                    Dim drg As New DragDataObject(eDragType.MoveStaffToDept,dvs.SelectedRows.Item(0))
                    dvs.DoDragDrop(drg,DragDropEffects.Move)
                End If
            End If
        End If
    End Sub

    Private Sub UpdateStaffDepartmentID(intCompanyID As Integer,intDepartmentID As Integer,lngStaffID As Long)
       '...
    End Sub

    Private Sub Tv_DragDrop(sender As Object,e As System.Windows.Forms.DragEventArgs) Handles Tv.DragDrop
        If e.Data.GetDataPresent(GetType(DragDataObject)) Then
            Dim drg As DragDataObject = e.Data.GetData(GetType(DragDataObject))
            If drg.DragType = eDragType.MoveStaffToDept Then
                Dim tnode As TreeNode = Tv.GetNodeAt(Tv.PointToClient(Control.MousePosition))
                UpdateStaffDepartmentID(FindNodeCompanyID(tnode),tnode.Name,CLng(CType(drg.Data,DataGridViewRow).Cells(TBC.lngStaffID).Value))
                Me.Dv.Rows.Remove(CType(drg.Data,DataGridViewRow))
                If dropNode IsNot nothing Then
                    EraserHighlightNode(dropNode)
                    dropNode = nothing
                End If
            End If
        End If
    End Sub

    Private Sub Tv_dragenter(sender As Object,e As System.Windows.Forms.DragEventArgs) Handles Tv.dragenter
        'If e.Data.GetDataPresent(GetType(DragDataObject)) Then
        '    Dim drg As DragDataObject = e.Data.GetData(GetType(DragDataObject))
        '    If drg.DragType = eDragType.MoveStaffToDept Then
        '        e.Effect = DragDropEffects.All
        '    Else
        '        e.Effect = DragDropEffects.None
        '    End If
        'End If
    End Sub

    Private Sub Tv_DragLeave(sender As Object,e As System.EventArgs) Handles Tv.DragLeave
        If dropNode IsNot nothing Then
            EraserHighlightNode(dropNode)
            dropNode = nothing
        End If
    End Sub

    Private Sub HighlightNode(node As TreeNode)
        With node
            .BackColor = SystemColors.Highlight
            .ForeColor = SystemColors.HighlightText
        End With
    End Sub

    Private Sub EraserHighlightNode(node As TreeNode)
        With node
            .BackColor = SystemColors.Window
            .ForeColor = SystemColors.WindowText
        End With
    End Sub

    Private Sub Tv_DragOver(sender As Object,e As System.Windows.Forms.DragEventArgs) Handles Tv.DragOver
        If e.Data.GetDataPresent(GetType(DragDataObject)) Then
            Dim drg As DragDataObject = e.Data.GetData(GetType(DragDataObject))
            If drg.DragType = eDragType.MoveStaffToDept Then
                Dim tnode As TreeNode = Tv.GetNodeAt(Tv.PointToClient(Control.MousePosition))
                If tnode IsNot nothing Then
                    If tnode.Tag = eTreeTag.Department Then
                        e.Effect = DragDropEffects.All
                    Else
                        e.Effect = DragDropEffects.None
                    End If
                Else
                    e.Effect = DragDropEffects.None
                End If
                If e.Effect = DragDropEffects.None Then
                    If dropNode IsNot nothing Then
                        EraserHighlightNode(dropNode)
                        dropNode = nothing
                    End If
                Else
                    If tnode IsNot nothing Then
                        If dropNode IsNot nothing Then
                            If tnode.Equals(dropNode) = False Then
                                EraserHighlightNode(dropNode)
                                dropNode = tnode
                                HighlightNode(dropNode)
                            End If
                        Else
                            dropNode = tnode
                            HighlightNode(dropNode)
                        End If
                    End If
                End If
            Else
                e.Effect = DragDropEffects.None
            End If
        End If
    End Sub

原文地址:https://www.jb51.cc/vb/258480.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐