Zdravím V desktopové aplikaci (VB2005) mám na Formu DataGridView, ve kterém chce uživatel přehazovat řádky pomocí metody Drag and Drop. Nagooglil jsem nějaký příklad, který mi ovšem nefunguje. Zde je kód:
Private dragBoxFromMouseDown As Rectangle
Private rowIndexFromMouseDown As Integer
Private rowIndexOfItemUnderMouseToDrop As Integer
Private Sub dgwScenar2_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles dgwScenar2.MouseMove
If (e.Button = MouseButtons.Right) Then
' If the mouse moves outside the rectangle, start the drag.
If (dragBoxFromMouseDown <> Rectangle.Empty And Not dragBoxFromMouseDown.Contains(e.X, e.Y)) Then
' Proceed with the drag and drop, passing in the list item.
Dim dropEffect As DragDropEffects = dgwScenar2.DoDragDrop(dgwScenar2.Rows(rowIndexFromMouseDown), DragDropEffects.Move)
End If
End If
End Sub
Private Sub dgwScenar2_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles dgwScenar2.MouseDown
' Get the index of the item the mouse is below.
If (e.Button = MouseButtons.Right) Then
rowIndexFromMouseDown = dgwScenar2.HitTest(e.X, e.Y).RowIndex
If (rowIndexFromMouseDown <> -1) Then
' Remember the point where the mouse down occurred.
' The DragSize indicates the size that the mouse can move before a drag event should be started.
Dim dragSize As Size = SystemInformation.DragSize
' Create a rectangle using the DragSize, with the mouse position being at the center of the rectangle.
dragBoxFromMouseDown = New Rectangle(New Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize)
Else
' Reset the rectangle if the mouse is not over an item in the ListBox.
dragBoxFromMouseDown = Rectangle.Empty
End If
End If
End Sub
Private Sub dgwScenar2_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles dgwScenar2.DragOver
e.Effect = DragDropEffects.Move
End Sub
Private Sub dgwScenar2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgwScenar2.DragEnter
e.Effect = DragDropEffects.Move
End Sub
Private Sub dgwScenar2_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles dgwScenar2.DragDrop
' The mouse locations are relative to the screen, so they must be converted to client coordinates.
Dim clientPoint As Point = dgwScenar2.PointToClient(New Point(e.X, e.Y))
' Get the row index of the item the mouse is below.
rowIndexOfItemUnderMouseToDrop = dgwScenar2.HitTest(clientPoint.X, clientPoint.Y).RowIndex()
' If the drag operation was a move then remove and insert the row.
If (e.Effect = DragDropEffects.Move) Then
Dim rowToMove As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
dgwScenar2.Rows.RemoveAt(rowIndexFromMouseDown)
dgwScenar2.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove)
End If
End Sub
Událost MouseDown nastane a po ní též událost MouseMove. V této události se vykoná kód, kde mě trochu mate, že proměnná dropEffect má po provedení příkazu
Dim dropEffect As DragDropEffects = dgwScenar2.DoDragDrop(dgwScenar2.Rows(rowIndexFromMouseDown), DragDropEffects.Move)
hodnotu DragDropEffects.None Pak už při tažení myši kamkoli v oblasti datagridview je zobrazen symbol, co připomíná značku zákaz stání, kterým se běžně v programech symbolizuje, že operace je zakázaná. Událost DragDrop nikdy nenastane. Věděl by někdo co s tím? Dík
|