Results 1 to 9 of 9

Thread: [RESOLVED] drag and drop

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    486

    Resolved [RESOLVED] drag and drop

    Hello
    I am trying to learn about drag and drop. I found a got to work dragging text from a textbox1 to a drop in a listbox1.
    I am now trying to drag the backcolor of textbox1 and drop it in textbox2. Lost would be an understatement.
    Here is the code
    Code:
    Public Class Form1
        Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
            TextBox1.DoDragDrop(TextBox1.BackColor, DragDropEffects.Copy)
        End Sub
        Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
            e.Effect = DragDropEffects.Copy
        End Sub
        Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
            TextBox2
        End Sub
    End Class
    I believe the first two subs to be correct. The drop in the third sub is where I am lost.
    Thank You
    George

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: drag and drop

    When you do drag and drop, whatever you pass in when you call DoDragDrop, you get back from e.Data in the DragDrop event handler. I suggest that you read this thread to learn how to do that regardless of the scenario. I suggest reading the whole thread but, if you read nothing else, be sure to read post #3.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: drag and drop

    Quote Originally Posted by georgesutfin View Post
    Hello
    I am trying to learn about drag and drop. I found a got to work dragging text from a textbox1 to a drop in a listbox1.
    I am now trying to drag the backcolor of textbox1 and drop it in textbox2. Lost would be an understatement.
    Here is the code
    Code:
    Public Class Form1
        Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
            TextBox1.DoDragDrop(TextBox1.BackColor, DragDropEffects.Copy)
        End Sub
        Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
            e.Effect = DragDropEffects.Copy
        End Sub
        Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
            TextBox2
        End Sub
    End Class
    I believe the first two subs to be correct. The drop in the third sub is where I am lost.
    Thank You
    George
    As JM told you, but here's the code...

    Code:
    Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
        If e.Data.GetDataPresent(GetType(Color)) Then
            TextBox2.BackColor = DirectCast(e.Data.GetData(GetType(Color)), Color)
        End If
    End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    486

    Re: drag and drop

    thank you .paul
    I have a better understanding now.
    Code:
    Public Class Form1
        Private Sub textBox1_mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms. MouseEventArgs) Handles TextBox1.MouseDown
            TextBox1.DoDragDrop(TextBox1.BackColor, DragDropEffects.Copy)
        End Sub
        Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
            If e.Data.GetDataPresent(GetType(Color)) Then
                TextBox2.BackColor = DirectCast(e.Data.GetData(GetType(Color)), Color)
            End If
        End Sub
    End Class
    However the second sub never fires.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: drag and drop

    If only someone had directed you to an existing thread that explains and demonstrates how the process works.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: drag and drop

    Quote Originally Posted by jmcilhinney View Post
    If only someone had directed you to an existing thread that explains and demonstrates how the process works.
    As JM is trying to tell you, read the thread. It's probably because you need AllowDrop and you need to handle DragEnter, etc...

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: drag and drop

    Here's the complete and tested working code...

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox2.AllowDrop = True
    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.BackColor, DragDropEffects.Copy)
        End If
    End Sub
    
    Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
        If e.AllowedEffect = DragDropEffects.Copy Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub
    
    Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
        If e.Data.GetDataPresent(GetType(Color)) Then
            TextBox2.BackColor = DirectCast(e.Data.GetData(GetType(Color)), Color)
        End If
    End Sub

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: drag and drop

    To get a smoother dragdrop effect, set AllowDrop for your form and all of your controls, then handle DragEnter for all of those too, by adding to the handles clause in TextBox2_DragEnter, which you should probably rename all_DragEnter. Your dragged cursor will show DragDropEffects.Copy on any control it passes over, including your form...
    Last edited by .paul.; Jun 26th, 2021 at 11:28 PM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    486

    Re: drag and drop

    .paul thank you for the working example.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width