S
H
A
R
E

Wednesday, July 13, 2011

VS2010 Drag-Drop Text in TextBox

Its Simple, Create new Windows Form Application using Visual Basic Language, and use one Textbox control. and then add code on Mouse Move event, Follow this:
In the form1 code, click on event and choose TextBox1

Select event (Mouse Move)
And you will get Code like this:
Now, Add the drag code:

If e.Button = Windows.Forms.MouseButtons.Left Then
   TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If

DoDragDrop is to send the TextBox1 text to another TextBox on other window where can accept Text Drag Drop, You can try it by dragging text in your TextBox to a browser, You Can't try it to a Notepad windows, because it can't accept.

By default, your TextBoxe is can't accept the dragdrop method, follow this to make a TextBox can accept the dragdrop method.

First, Set Allow Drop To TRUE
Add this code to the Form1 code:

Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
        TextBox1.Text = e.Data.GetData(System.Windows.Forms.DataFormats.StringFormat)
End Sub

Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
        e.Effect = DragDropEffects.Copy
End Sub

Finally, Your form code should be like this:

ublic Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
        TextBox1.Text = e.Data.GetData(System.Windows.Forms.DataFormats.StringFormat)
    End Sub

    Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
        e.Effect = DragDropEffects.Copy
    End Sub

    Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
        End If
    End Sub
End Class

3 comments:

College Essays said...

Thanks for linking this up....Love your blog.. I have bookmarked it so that I can come back & read more..

Klampok Child said...

Thanks again, I'm happy to have visitors like you. If you have an opinion, I am willing to accept it.

Mobile App Developers said...

It was very useful for me. Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing the such information with us.

Post a Comment