Drag and Drop Formatted Text to External Application

larkahn

Member
Joined
Sep 10, 2006
Messages
18
Programming Experience
1-3
I have one RichTextBox with formatted text. I have drag and drop enabled so that I can drag the selected text if the right mouse button is down. The problem is that the formatting is not copied or moved with the text to an external application such as MS Word XP or WordPad. In particular, I want the text with the particular font that the RTF box is set for. All that happens now is that plain text is moved or copied. I don't want to have to reset the font manually after I transfer the text. I have Windows XP and MS Word XP. How can I have the formatted text dropped. If I changed Selected text to RTF, that simply copies the formatting codes. I'm not a programmer. Is there an easy way to modify the following code?

VB.NET:
If e.Button = MouseButtons.Right Then
RichTextBox1.DoDragDrop(RichTextBox1.SelectedText, DragDropEffects.Copy)
End If
 
example:
VB.NET:
Dim data_object As New DataObject
data_object.SetData(DataFormats.Rtf, rtb.SelectedRtf)
data_object.SetData(DataFormats.Text, rtb.SelectedText)
rtb.DoDragDrop(data_object, DragDropEffects.Copy)
 
Thank you. This works fine. One final question: Could you tell me how I would code my if statement so that the user has to press the Alt+left mouse key at the same time to initiate the drag?
 
You can keep track of your ALT key state with the KeyDown and KeyUp events, the info is in e.Alt parameter. You will also find out that KeyUp event isn't triggered if drag&drop occurs in the mean time.
 
Thanks again. your help is most appreciated. I placed the following code in the keydown event of the RTF Box. It seems to work but I'm wondering if this is the correct place and coding. In this case, there doesn't seem to be any need for coding the mouse button.

VB.NET:
If e.Alt = True Then
     Dim data_object As New DataObject()
     data_object.SetData(DataFormats.Rtf, RichTextBox1.Rtf)
     data_object.SetData(DataFormats.Text, RichTextBox1.Text)
     RichTextBox1.DoDragDrop(data_object, DragDropEffects.Move)
End If
 
No, that won't work. You have to capture the ALT state in the KeyDown event, then implement the dragdrop from MouseDown accordingly.
 
I'm sorry. I'm pretty bad at this basic coding. Could I trouble you as to how the capture is coded in the KeyDown event and then implemented in the MouseDown? The coding that I did insert above does seem to work.
 
Last edited:
If I do your example the selection is clearing upon ALT keydown.

Declare a variable as Boolean (which is the state of the ALT key) outside these methods (or anywhere global you may want to store this information) to make it available to them all, use this variable to set/get the ALT state in the different methods.
 
I feel, though, that I'm not using the e.Alt parameter correctly. If I hold down the Alt key and drag with the mouse, the text is dragged and dropped ok; however, after the drop, the drag is still active -- I can drag from the text box again without pressing the Alt key. This means I can no longer select text. If, though, I press the Alt key by itself, it seems to toggle dragdrop off. I think what is happening is that after I drop in MS Word, the focus is no longer in the RichTextBox, so the KeyUp event won't fire. If, though, 1) I drag to MS Word 2) drop by only releasing the mouse button while keeping the Alt key depressed 3) click in the RichTextBox and then release the Alt key -- then the KeyUp event will fire properly. Is this what is happening, and is there a way around this?

I declared on the form:

VB.NET:
 Public Shared myAltDown As Boolean

In the KeyDown event:

VB.NET:
If e.Alt = True Then
            myAltDown = e.Alt.TrueString  
End If

In the KeyUp event:
VB.NET:
If e.Alt = False Then
            myAltDown = e.Alt.FalseString
End If

In the MouseDown event:


VB.NET:
Dim data_object As New DataObject()
        data_object.SetData(DataFormats.Rtf, RichTextBox1.Rtf)
        data_object.SetData(DataFormats.Text, RichTextBox1.Text)
        If e.Button = MouseButtons.Left And myAltDown = True Then
            RichTextBox1.DoDragDrop(data_object, DragDropEffects.All)
        End If
 
Last edited:
The way around is simple, since you know KeyUp don't happen when you start dodragdrop just set myAltDown to False first, it has served its purpose.

Drop the "truestring" stuff, e.alt is a Boolean. myAltDown should be Private, not Public and not Shared.
 
Thank you again. It does seem to work. Is the following correct:

VB.NET:
Private myAltDown As Boolean

RichTextBox1_KeyDown

VB.NET:
If e.Alt = True Then
     myAltDown = e.Alt
End If

RichTextBox1_MouseDown

Dim data_object AsNew DataObject()
data_object.SetData(DataFormats.Rtf, RichTextBox1.Rtf)
data_object.SetData(DataFormats.Text, RichTextBox1.Text)
If e.Button = MouseButtons.Left And myAltDown = TrueThen
myAltDown = False
RichTextBox1.DoDragDrop(data_object, DragDropEffects.All)
EndIf
 
Yes, but you want to set myAltDown to False in KeyUp also (like previous code), in case the key was used and there wasn't an dragdrop, else your whole use of this ALT key is pretty useless.
 
Thanks again. You've made me sweat by not giving me all the information at once or spoon feeding me with the entire code (with the exception of setting the data format) which would have made this a lot easier. As a teacher, I appreciate the value of making a student work by giving just enough guidance.
 
Back
Top