Question Textbox with dragdrop

Pirahnaplant

Well-known member
Joined
Mar 29, 2009
Messages
75
Programming Experience
3-5
I am trying to make a textbox with complete support for dragdrop, however, it doesn't work. Could anyone help me here?

VB.NET:
    Private MouseIsDown As Boolean = False
    Private SelectionLen As Integer
    Private SelectStart As Integer

    Private Sub TxtBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TxtBox.MouseDown
        'Find the point that is the start of the selected text
        Dim minpos As Integer = TxtBox.CreateGraphics.MeasureString(Mid(TxtBox.Text, 1, SelectStart), TxtBox.Font).Width
        'Find the point that is the end of the selected text
        Dim maxpos As Integer = TxtBox.CreateGraphics.MeasureString(Mid(TxtBox.Text, 1, SelectStart + SelectionLen), TxtBox.Font).Width
        'Check if text is selected and if the user has clicked the selected text
        If SelectionLen And e.X > minpos And e.X < maxpos Then
            'Set the mouse down flag used to check if drag drop should be initiated
            MouseIsDown = True
        End If
        TxtBox.SelectionLength = 0
    End Sub

    Private Sub TxtBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TxtBox.MouseMove
        'If the user is dragging the selected text, initiate the drag drop
        If MouseIsDown Then
            'Restore the original selected text
            TxtBox.SelectStart = SelectStart
            TxtBox.SelectionLength = SelectionLen
            'Start a drag drop
            TxtBox.DoDragDrop(Mid(TxtBox.Text, SelectStart + 1, SelectionLen), DragDropEffects.Copy)
            MouseIsDown = False
        End If
        'Save the selected text
        SelectionLen = TxtBox.SelectionLength
        SelectStart = TxtBox.SelectStart
    End Sub

    Private Sub TxtBoxp_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TxtBox.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            'Show the copy cursor
            e.Effect = DragDropEffects.Copy
        Else
            'Show the can't copy cursor
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub TxtBoxDragDrop_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TxtBox.DragDrop
        'Paste the dragged text into the textbox
        TxtBox.Paste(e.Data.GetData(DataFormats.Text))
    End Sub
 
Back
Top