OLE DragDrop - Drops onto It's Self!

How can I keep the following routine from dropping it's own selected text
onto itself?  It's a Rich Text Box. When I select some text and drag the mouse it shows that it's dragging it.  Then when I release the mouse
button it drops it on itself and deletes everything but the selected text!

It's supposed to just be able to drag-n-drop from other apps (and vice versa). How can I say:

IF Source = YourOwnDamnSelf AND Target = YourOwnDamnSelf  then
     Exit Sub
End IF

Private Sub rtbCodeWindow_OLEDragDrop(Data As RichTextLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Data.GetFormat(vbCFText) Then  'if text
        If (Effect And vbDropEffectCopy) Then
            rtbCodeWindow.TextRTF = Data.GetData(vbCFText) 'set the window to the dragged in text
            rtbCodeWindow.LoadFile Data.GetData(vbCFText)  'open the dragged in file
        End If
    End If
       If Data.GetFormat(vbCFFiles) Then        'if files from explorer
        rtbCodeWindow.LoadFile Data.Files(1) 'open the file dragged from windows
    End If
End Sub
Who is Participating?
AzraSoundConnect With a Mentor Commented:
but the locked thing worked for me, adding these two pieces of code:

Private Sub RichTextBox1_OLECompleteDrag(Effect As Long)
    RichTextBox1.Locked = False
End Sub

Private Sub RichTextBox1_OLEStartDrag(Data As RichTextLib.DataObject, AllowedEffects As Long)
    RichTextBox1.Locked = True
End Sub

is there ever a time you actually want anything to be dropped into the rtb?  if not then set its oledropmode to rtfoledropnone
jgoreAuthor Commented:
Yes! I want to be able to drag files to it!
There are lots of times I want to drag stuff over to
it. I certainly don't want to turn DragDrop off!

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

how bout just setting its .Locked property to true at the beginning of the drag event and false when its done, i.e. in the olecompletedrag event
jgoreAuthor Commented:
I think thats just going to effect the edit-ability
of the Rich Text Box.

..Locked :
True -  You can scroll and highlight the text in the control, but you can't edit it. The program can still modify the text by changing the Text property.
False - (Default) You can edit the text in the control.
i just used your code and it didnt do what you say its doing to you...when i selected text from the rtb and dropped it back into the rtb, nothing happened.
oh never mind i had to set my drop mode to manual
jgoreAuthor Commented:
Hmmm...It does work. Very nice! THANKS!
I wonder why it works? heh. VB says it just locks editing. It also says that using the text property
should still work.

rtbCodeWindow.TextRTF = Data.GetData(vbCFText)  

Should still work. I guess I'm just reading it
wrong! I tried dragging and dropping different
stuff and it all still worked but now it won't drop
to itself, thank god! It was a real editing nightmare the other way! Thanks again!
i think it would still change if you had a command button and said rtb.text = "text" but since its locked, part of what it blocks is the ability to drop items onto it, thus it will never get to your code that sets the text programmatically. well for whatever reason, glad its working for ya.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.