Datagridview to tabcontrol dragdrop Winforms

Datagridview to tabcontrol dragdrop Winforms

我需要完成的是让用户可以从 datagridview 拖放行并将鼠标悬停在不同的标签页上,然后我将这些行添加到该标签页上的 datagridview。

Private Sub datagridview_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagridview.MouseDown
    mouseDownPosition = e.Location
End Sub

Private Sub datagridview_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagridview.MouseMove
    If CheckMouseMovement(sender, datagridview, e) Then
        listofBuilds = New List(Of Build)
        For Each row As DataGridViewRow In dataGridView.Rows
            If Convert.ToBoolean(row.Cells.Item(0).Value) Then
                Dim t As Build = DirectCast(row.DataBoundItem, Build)
                listofBuilds.Add(t)
            End If
        Next
        If listofBuilds.Count > 0 Then
            dataGridView.DoDragDrop(sender, dropEffect)
        End If
    End If
End Sub

Private Sub TabControl_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragEnter
    e.Effect = DragDropEffects.All
End Sub

Private Sub TabControl_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragDrop
    Dim DropPage As TabPage = GetTabPageByTab(TabControl.PointToClient(New Point(e.X, e.Y)))
    If DropPage IsNot TabControl.SelectedTab Then
        If DropPage Is Page1 Then
        If DropPage Is Page2 Then
        If DropPage Is Page3 Then
           //etc
        End If
    End If
End Sub

Private Function GetTabPageByTab(ByVal point As Point) As TabPage
    For i As Integer = 0 To TabControl.TabPages.Count - 1
        If TabControl.GetTabRect(i).Contains(point) Then
            Return TabControl.TabPages.Item(i)
        End If
    Next
    Return Nothing
End Function

它永远不会进入我的代码的拖动输入部分,我发现这是因为我忘记通过设置 AllowDrop = true 来允许每个页面的拖放。我通过在 tabcontrol 中获取基于 X 和 Y 坐标的标签页来解决我的问题。希望这对其他人有用。

这段代码对我有用,可以将数据从一个页面移动到另一个页面

Private Sub TabControl_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragEnter
   e.Effect = DragDropEffects.All
End Sub

Private Sub TabControl_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragDrop
    Dim DropPage As TabPage = GetTabPageByTab(TabControl.PointToClient(New Point(e.X, e.Y)))
    If DropPage IsNot TabControl.SelectedTab Then
       If DropPage Is Page1 Then
       If DropPage Is Page2 Then
       If DropPage Is Page3 Then
          //etc
       End If
    End If
End Sub

Private Function GetTabPageByTab(ByVal point As Point) As TabPage
   For i As Integer = 0 To TabControl.TabPages.Count - 1
       If TabControl.GetTabRect(i).Contains(point) Then
           Return TabControl.TabPages.Item(i)
       End If
   Next
   Return Nothing
End Function