在拖放过程中显示文字下降

时间:2017-04-04 08:29:42

标签: vb.net winforms drag-and-drop

我们的PO想要显示在两个列表框之间拖动的文本。 我没有找到任何线索如何在开箱即用的窗体上执行此操作。 我找到了this文章,但它在表单上使用了额外的标签。这真的是最好的唯一选择吗?

1 个答案:

答案 0 :(得分:1)

根据我上面的评论,Code Project上有一个示例,除了能够在搜索中找到它之外,我无法获得任何信用,所有信用都归于代码项目中的Elkay。

以下是复制和粘贴的代码,以防链接在将来中断。

Dim myCursor As TextCursor.xCursor = New TextCursor.xCursor
myCursor.CursorText = "This is a test cursor"
Me.Cursor = myCursor.GetCursor
Private _Dragging As Boolean        ' Indicates that Dragging has begun
Private _DragSource As Integer = 0      ' The source of the drag

' Here's my custom cursor
Private myCursor As TextCursor.xCursor = New TextCursor.xCursor

''' <summary>
''' Begin our Dragging - setup a few cursor properties
''' </summary>
Private Sub DragLabel_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles DragLabel.MouseDown
    _Dragging = True

    myCursor.Shrinkage = 1
    myCursor.Fade = True
    myCursor.Font = DragLabel.Font
    myCursor.CursorText = DragLabel.Text

End Sub

''' <summary>
''' If dragging has begun, fire off the dragdrop and stuff the Object
''' </summary>
Private Sub DragLabel_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles DragLabel.MouseMove
    If _Dragging Then
        _DragSource = 1
        DragLabel.DoDragDrop(DragLabel.Text, DragDropEffects.Copy)
    End If
    _Dragging = False
End Sub

''' <summary>
''' If you don't do this, you'll get the standard "You can't drop here" cursor
''' </summary>
Private Sub DragLabel_DragOver(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) Handles DragLabel.DragOver
    If e.Data.GetDataPresent(DataFormats.StringFormat) Then
        e.Effect = DragDropEffects.None
    End If
End Sub

''' <summary>
''' This bad boy is DragDrop UI's Golden Child.  
''' This will fire during the dragging operation
''' (once the DoDragDrop method has been started) and allow you to trap while dragging
'''
''' In the case of this Demo - I'm checking to see if we have a valid drop location:
''' we'll have both an effect AND a true Copy condition set.
''' </summary>
Private Sub DragLabel_GiveFeedback(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles DragLabel.GiveFeedback
    e.UseDefaultCursors = False
    If ((e.Effect And DragDropEffects.Copy) = DragDropEffects.Copy) Then
        myCursor.GoodDrop = TextCursors.xCursor.DropValid.GoodDrop
        Cursor.Current = myCursor.GetCursor
    Else
        myCursor.GoodDrop = TextCursors.xCursor.DropValid.BadDrop
        Cursor.Current = myCursor.GetCursor
    End If
End Sub

''' <summary>
''' Let the ap know we're over a good drop location
''' Share that knowledge with the User by changing our Drag Cursor and 
''' alter the drop control's bg color
''' </summary>
Private Sub DropLabel_DragOver(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) Handles DropLabel.DragOver
    If e.Data.GetDataPresent(DataFormats.StringFormat) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub

''' <summary>
''' Perform the drop - if anyone knows a more intelligent way to determine 
''' WHERE the drag CAME FROM
''' I would love to hear about it!
''' </summary>
Private Sub DropLabel_DragDrop(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) Handles DropLabel.DragDrop
    If (e.Data.GetDataPresent(GetType(System.String))) Then
        Dim item As Object = CType(e.Data.GetData(GetType(System.String)), _
                                System.Object)
        If _DragSource = 2 Then
            DropLabel.Font = AnotherDrag.Font
        Else
            DropLabel.Font = DragLabel.Font
        End If
        DropLabel.Text = item.ToString
    End If
End Sub