【问题标题】:Display text during drag & drop在拖放期间显示文本
【发布时间】:2017-04-04 08:29:41
【问题描述】:

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

【问题讨论】:

  • 论坛的前2个答案都没有使用标签,他们只是使用SelectedItem.ToString?
  • DragMessage 对象是一个标签控件。
  • 从未尝试过,因此评论而不是答案,但有一篇关于Code Project - Text Cursor (xCursor)的文章。
  • 哦,我的错,错过了那一点。这是一种非常简单有效的方法 - 这样做有什么问题?
  • @OSKM:谢谢,它有效,所以你可以把它作为答案:) 但我仍然不敢相信这一定如此复杂。

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


【解决方案1】:

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

下面是复制粘贴的代码,以防万一以后链接断开。

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

【讨论】:

  • 我会保留它作为答案,因为它确实,它承诺的,但是这个解决方案有一个意想不到的副作用:我的列表框,也就是源,在拖放后表现得很奇怪:滚动后不会重绘列表。滚动条可以工作,我仍然可以向下、选择、拖放最后一个项目(并在拖动过程中看到标题),只是我没有看到滚动项目和列表内的选择。
猜你喜欢
  • 1970-01-01
  • 2013-12-02
  • 2018-05-01
  • 2023-03-18
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多