拖放:复制而不是移动项目

时间:2011-10-25 13:29:40

标签: c# vb.net

我有两个(Telerik)TreeView控件,我们称之为tvSource和tvDest。我想将项目从tvSource拖放到tvDest。我有点工作,但遇到两个问题:

1)它现在做的是移动物品。相反,我希望将它们复制(从tvSource复制到tvDest),将项目保留在tvSource中。

2)要允许从tvSource控件拖动项目,我必须启用DragDrop。启用此功能后,用户可以在tvSource控件内拖放,这不是我想要的。这些项目只能被拖动(复制)到另一个控件(tvDest)。

到目前为止,我的代码如下:

Private blnMouseIsDown As Boolean = False

Private Sub tvSource_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tvSource.MouseDown
    blnMouseIsDown = True
End Sub

Private Sub tvDest_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles tvDest.DragDrop
    Dim p As Point = tvDest.PointToClient(New Point(e.X, e.Y))
    Dim hoverNode As RadTreeNode = tvDest.GetNodeAt(p.X, p.Y)
    If hoverNode Is Nothing Then
        tvDest.Nodes.Add(e.Data.GetData(DataFormats.Text).ToString())
        Return
    End If
End Sub

Private Sub tvDest_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles tvDest.DragEnter
    If e.Data.GetDataPresent(DataFormats.Text) Then
        e.Effect = DragDropEffects.Copy
    Else
        e.Effect = DragDropEffects.None
    End If
End Sub

Private Sub tvSource_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tvSource.MouseMove

    If blnMouseIsDown Then
        tvSource.DoDragDrop(tvSource.SelectedNode.Name, DragDropEffects.Copy)
    End If
    blnMouseIsDown = False

End Sub

Private Sub tvSource_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tvSource.MouseDown blnMouseIsDown = True End Sub Private Sub tvDest_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles tvDest.DragDrop Dim p As Point = tvDest.PointToClient(New Point(e.X, e.Y)) Dim hoverNode As RadTreeNode = tvDest.GetNodeAt(p.X, p.Y) If hoverNode Is Nothing Then tvDest.Nodes.Add(e.Data.GetData(DataFormats.Text).ToString()) Return End If End Sub Private Sub tvDest_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles tvDest.DragEnter If e.Data.GetDataPresent(DataFormats.Text) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub tvSource_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tvSource.MouseMove If blnMouseIsDown Then tvSource.DoDragDrop(tvSource.SelectedNode.Name, DragDropEffects.Copy) End If blnMouseIsDown = False End Sub

1 个答案:

答案 0 :(得分:0)

在他们的支持网站Enabling Drag and Drop,看起来如果您启用了属性AllowDragDrop,那么它将默认执行完整移动(Hans指出)。尝试将其设置为false。

对于tvDest,请启用AllowDrop属性,该属性允许使用标准窗口拖放功能。

另外,我认为没有必要使用blnMouseIsDown变量或MouseMove事件。将DoDragDrop方法移动到MouseDown事件。