locked
How do I allow other applications to drag-and-drop from mine? RRS feed

  • Question

  • WHAT I HAVE:

    Visual Basic 2010, .NET 4.0, WinForms

    MY PROBLEM:

    The Visual Studio Help explains how easy it is to move information within an application or to "import" it from another app using "drag-and-drop". The problem is, how do I configure info in my app so that it can be "exported"--that is, allow another app to drag and drop info from my app? That's not explained. For instance, VS Help has an example of how text entered in WordPad can be selected, then dropped into a TextBox or RichTextBox in a one's VB app. But what if one wants to do the reverse: make the contents of a TextBox/RichTextBox in the VB app available to select, then drop into WordPad? (Bear in mind that the issue here isn't "text"--it's drag-and-drop logic in general.)


    Robert Gustafson

    Monday, December 1, 2014 3:11 AM

Answers

  • But what if one wants to do the reverse: make the contents of a TextBox/RichTextBox in the VB app available to select, then drop into WordPad?

    The code for the source control is the same regardless of whether the destination is a control in the same application or a control in a different application.  The issue with a text box might be that the left mouse button is not a good choice for initiating the process.  This code:

        Private MouseIsDown As Boolean = False
    
        Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
            If e.Button = MouseButtons.Right Then ' Set a flag to show that the mouse is down.
                MouseIsDown = True
            End If
        End Sub
    
        Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
            If MouseIsDown Then
                ' Initiate dragging.
                TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
            End If
            MouseIsDown = False
        End Sub
    starts a dragging operation that doesn't know or care where you drag that selected text to.

    • Proposed as answer by Mr. Monkeyboy Monday, December 1, 2014 1:09 PM
    • Marked as answer by RobertGustafson Wednesday, December 3, 2014 12:33 AM
    Monday, December 1, 2014 4:05 AM
  • Hi,

     To add to what Mr. Monkeyboy said, the other application may not support the Data type you are dragging ether. For example, the following code will drag an Image from PictureBox1 which will work fine for Windows Wordpad but, my image editing program does not recognize the Data type being dragged.

    Public Class Form1
        Private StartDrag As Boolean = False
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PictureBox1.Image = Image.FromFile("C:\TestFolder\MyCat.jpg")
        End Sub
    
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            StartDrag = (e.Button = MouseButtons.Left)
        End Sub
    
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = MouseButtons.Left And StartDrag Then
                PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy)
            End If
            StartDrag = False
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 1, 2014 2:32 PM
    • Marked as answer by RobertGustafson Wednesday, December 3, 2014 12:32 AM
    Monday, December 1, 2014 1:59 PM

All replies

  • But what if one wants to do the reverse: make the contents of a TextBox/RichTextBox in the VB app available to select, then drop into WordPad?

    The code for the source control is the same regardless of whether the destination is a control in the same application or a control in a different application.  The issue with a text box might be that the left mouse button is not a good choice for initiating the process.  This code:

        Private MouseIsDown As Boolean = False
    
        Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
            If e.Button = MouseButtons.Right Then ' Set a flag to show that the mouse is down.
                MouseIsDown = True
            End If
        End Sub
    
        Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
            If MouseIsDown Then
                ' Initiate dragging.
                TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
            End If
            MouseIsDown = False
        End Sub
    starts a dragging operation that doesn't know or care where you drag that selected text to.

    • Proposed as answer by Mr. Monkeyboy Monday, December 1, 2014 1:09 PM
    • Marked as answer by RobertGustafson Wednesday, December 3, 2014 12:33 AM
    Monday, December 1, 2014 4:05 AM
  • WHAT I HAVE:

    Visual Basic 2010, .NET 4.0, WinForms

    MY PROBLEM:

    The Visual Studio Help explains how easy it is to move information within an application or to "import" it from another app using "drag-and-drop". The problem is, how do I configure info in my app so that it can be "exported"--that is, allow another app to drag and drop info from my app? That's not explained. For instance, VS Help has an example of how text entered in WordPad can be selected, then dropped into a TextBox or RichTextBox in a one's VB app. But what if one wants to do the reverse: make the contents of a TextBox/RichTextBox in the VB app available to select, then drop into WordPad? (Bear in mind that the issue here isn't "text"--it's drag-and-drop logic in general.)


    Robert Gustafson

    Your app has to allow the drag and drop. That doesn't mean the other app you want to drag and drop to necessarily supports that capability.


    La vida loca

    • Proposed as answer by Mr. Monkeyboy Monday, December 1, 2014 1:09 PM
    • Unproposed as answer by Mr. Monkeyboy Monday, December 1, 2014 1:09 PM
    Monday, December 1, 2014 1:08 PM
  • Hi,

     To add to what Mr. Monkeyboy said, the other application may not support the Data type you are dragging ether. For example, the following code will drag an Image from PictureBox1 which will work fine for Windows Wordpad but, my image editing program does not recognize the Data type being dragged.

    Public Class Form1
        Private StartDrag As Boolean = False
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PictureBox1.Image = Image.FromFile("C:\TestFolder\MyCat.jpg")
        End Sub
    
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            StartDrag = (e.Button = MouseButtons.Left)
        End Sub
    
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = MouseButtons.Left And StartDrag Then
                PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy)
            End If
            StartDrag = False
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 1, 2014 2:32 PM
    • Marked as answer by RobertGustafson Wednesday, December 3, 2014 12:32 AM
    Monday, December 1, 2014 1:59 PM
  • My question is simply how does my application ALLOW specific info to be dragged and dropped onto other applications. Whether the other app will "import" my app's info is of course the other app's business. Just how do I make info AVAILABLE for d&d?

    Robert Gustafson

    Tuesday, December 2, 2014 2:10 AM
  • My question is simply how does my application ALLOW specific info to be dragged and dropped onto other applications.

    You make the information available using the code above, adjusted for the source of the information and the type of the information, as appropriate.

    What is the code you are using and what is the problem you have with implementing drag and drop?

    Tuesday, December 2, 2014 4:01 AM
  • My problem yet; I was just wondering how it's done.


    Robert Gustafson

    Wednesday, December 3, 2014 12:31 AM