0

我有将文件拖入文本框的代码,但我也想有代码将文件拖出文本框,从而将其清除。

有人有什么想法吗?

4

2 回答 2

0

检查 TextBox 上的 DragLeave 事件 如果离开应用程序,最简单的方法可能是将其存储为变量或剪贴板。

    Private Sub TheTextBox_DragLeave(sender As System.Object, e As System.EventArgs) Handles TheTextBox.DragLeave
Clipboard.SetText(TheTextBox.Text)
TheTextBox.Clear() 'Optional

End Sub

然后,您当然需要对鼠标向上或拖入或其他任何内容时发生的情况进行编码。您可以在这些步骤之一中清除文本框。取决于您的实施。

于 2013-10-11T20:55:05.553 回答
0

您可以使用该DoDragDrop方法进行拖放操作。它返回一个DragDropEffects值,该值指定数据是否实际上已被删除,在这种情况下您可以清除文本框。

由于在按下鼠标按钮时鼠标移动一点之前不应开始拖放操作,因此您需要在MouseDownandMouseMove事件中检查它。SystemInformation.DragSize告诉您在拖放操作开始之前鼠标应该移动多远。

MouseDown事件中检查您是否真的要开始拖动(即按下左键并且文本框实际上包含文本)。然后使用鼠标位置和给定的大小创建一个矩形SystemInformation.DragSize。在MouseMove事件中检查鼠标是否被拖动到矩形之外并调用DoDragDrop

Private _dragStart As Boolean
Private _dragBox As Rectangle

Private Sub srcTextBox_MouseDown(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseDown
    ' a drag starts if the left mouse button is pressed and the text box actually contains any text
    _dragStart = e.Button = MouseButtons.Left And Not String.IsNullOrEmpty(srcTextBox.Text)

    If _dragStart Then
        Dim dragSize As Size = SystemInformation.DragSize
        _dragBox = New Rectangle(New Point(e.X - (dragSize.Width \ 2),
                                           e.Y - (dragSize.Height \ 2)), dragSize)
    End If
End Sub

Private Sub srcTextBox_MouseUp(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseUp
    _dragStart = False
End Sub

Private Sub srcTextBox_MouseMove(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseMove
    If Not _dragStart Or
       (e.Button And MouseButtons.Left) <> MouseButtons.Left Or
       _dragBox.Contains(e.X, e.Y) Then Return

    Dim data As New DataObject()
    data.SetData(srcTextBox.Text)
    ' you can optionally add more formats required by valid drag destinations:
    ' data.SetData(DataFormats.UnicodeText, Encoding.Unicode.GetBytes(srcTextBox.Text))
    ' data.SetData("UTF-8", Encoding.UTF8.GetBytes(srcTextBox.Text))
    ' data.SetData("UTF-32", Encoding.UTF32.GetBytes(srcTextBox.Text))

    Dim dropEffect As DragDropEffects = srcTextBox.DoDragDrop(data, DragDropEffects.Move)
    If (dropEffect = DragDropEffects.Move) Then
        srcTextBox.Text = ""
    End If

    _dragStart = False
    _dragBox = Rectangle.Empty
End Sub

Private Sub destTextBox_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragOver
    If e.Data.GetDataPresent(GetType(String)) Then
        e.Effect = e.AllowedEffect And DragDropEffects.Move
    Else
        e.Effect = DragDropEffects.None
    End If
End Sub

Private Sub destTextBox_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragDrop
    If e.Data.GetDataPresent(GetType(String)) Then
        destTextBox.Text = e.Data.GetData(GetType(String)).ToString()
    End If
End Sub

在 TextBox 中拖动鼠标通常会启动文本选择。上面的代码改变了这种行为。用户不能再使用鼠标来选择文本。这显然不是一个好主意,因为用户不会想到这一点。要允许使用鼠标和拖动进行文本选择,您需要控制选择机制。这意味着您需要创建自己的文本框类。

我会建议一种不同的方法:使用将值显示为拖动源和/或目标的标签。要允许编辑,您可以创建一个隐藏的文本框。如果用户双击标签,则隐藏标签并显示文本框。用户完成编辑后(通过按回车键或取消键)或者如果文本框失去焦点,则隐藏文本框并再次显示标签。

于 2013-10-11T23:53:21.090 回答