views:

16

answers:

2

hi,

I want to achieve the following:

The user drags text from any open window not related to my application ( like firefox or word, for example) onto button1 on form1 in my application. when he/she does that, a new form (called form2 that contains a richtextbox) will open and the dragged text is directly copied (or inserted) into the richtextbox of the new form. button1 has allowdrop set to true. Beyond that I don't know how to proceed.

I tried:

e.effects = DragDropEffects.Copy

But it seems it is not enough. Could you help please?

Thanks

A: 
Josaph
thanks for your response. I am aware of the above mentioned method and already read the articles you posted. However, this does not help me. the code you wrote and the articles you posted assume you want to drag-drop from one control on your form to another. What I want is different (as described in my original question). But thanks, anyway, for your help.
mazrabul
A: 

I figured it out. I'm sharing so others might benefit.

First, I declared a global variable in one of the modules:

Public draggedText As String = ""

Second, I handled the dragdrop event on the button as follows:

Private Sub button1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles button1.DragDrop
    draggedText = e.Data.GetData(DataFormats.Text)
    frm_form2.Show()
End Sub

Third, in the load event of frm_form2 I added the following:

If draggedText <> "" Then
        richTextBox1.Text = draggedText
        draggedText = ""
End If

That's it. Not as complicated as I thought. Also, you can add the code for the dragEnter event mentioned in the previous answer to change how the cursor looks.

I hope this helps.

mazrabul