Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello, sadly I'm a noob, and I can't manage to find an event that occurs after I drop a file in a RichTextBox.

When you drop a file into a Rtb the icon's image is copied on to the control (if you downloaded this control which derives from richtextbox, this control has a few methods in which manipulate RTF such as AppendRtf(_rtf)).

Now I want to present you with a scenario wich works.
1) I drag a file into the Rtb, I check if it's a file being dragged onto the control. If true, I render all effects.
C#
void txt_send_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
    txt_send.Clear();
    if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
    {
        e.Effect = DragDropEffects.All;
    } 
}

2) I drop the file, here for this secnario all I do is update a boolean value that indicates the file as been dropped.
C#
void txt_send_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
    File_droped = true;           
}

After this event (2) an image is inserted into the Rtb.

3) Finally, in order to test the issue I presented above, I call the MouseUp event , where I copy that image's RTF(Rich Text Format).

So now I click the control , and the following event is dispatched.

I just want to clarify, I'm aware that MouseUp does not occur after the DragDrop event, I only added it to test the operation I want to perform to ensure it works.
C#
void txt_send_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (File_droped)
    {
        txt_main.AppendRtf(txt_send.Rtf); 
    } 
}

Now the file's icon (Image) is copied to anouther RichTextBox (THIS WORKS).

Now again what I'm looking for is an actual event that occurs after the DragDrop event, becuase I want this to perform after DragDrop has ended.
If I attempt to do this from inside the event, it won't work because the image only appears after the event .
C#
txt_main.AppendRtf(txt_send.Rtf);
Posted
Updated 5-Feb-11 5:24am
v5

Maybe some other event could be used with somesort of flag to indicate that there needs to be something done this time (otherwise it might do things you don't want). I haven't tested it but could imagine

VB
Private Sub BindingContext_Changed(sender As Object, e As EventArgs)
   if(flag_append_rtf_after_drop) {
       txt_main.AppendRtf(txt_send.Rtf);
   }
End Sub

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bindingcontextchanged.aspx[^]

Could also be another event since I didn't actually test this, but you could have a look and try some of them:
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_events.aspx[^]

Also, I thought that there was a DragEnd event but could only find it for some controls. Maybe the event is still raised but simply not implemented or exposed as an event. Maybe it could be implemented by overriding the RaiseDragEvent or OnNotifyMessage but also haven't tested it. But could also be an idea to have a look at,

Well, hopefully this gives you some help. Another way could be simply by using a timer that you enable after the dragdrop and do it there. Maybe an Application.DoEvents(); in the dragdrop method between the lines could maybe also help (maybe even considered valid in this case). But aside from that, these are the less elegant ways to handle it but could also be worth exploring.

Good luck!
 
Share this answer
 
i ended up using the text_changed event with a flag that indiactes that a file was droped ..

private void txt_send_TextChanged(object sender, EventArgs e)
{
if (file_droped_flag)
{
current_content.userTextBox.AppendText("\n\n");

current_content.userTextBox.AppendRtf(txt_send.Rtf);
current_content.userTextBox.AppendText("\n");
current_content.userTextBox.AppendTextAsRtf("Sending file request",new Font(this.Font, FontStyle.Underline | FontStyle.Bold), RtfColor.Blue, RtfColor.Yellow);
current_content.userTextBox.AppendText("\n\n");
file_droped_flag = false;
txt_send.Clear();
}
}

the problem is that this event happends frequntly , is there a was other then a flag to check if the event was relavent , to raise an event only on certine conditions ?
 
Share this answer
 
Comments
E.F. Nijboer 7-Feb-11 14:36pm    
You are lucky I see this comment (added as an answer). It is advised to add such comments as reply to an answer because then that person will get an update by mail.

Well, about your problem. I understand you don't like the code construction using a flag because it looks somewhat like a hack. You can simply hide this by introducing your own new event. You create your own descendant of a richtextbox. You declare a private property there that you can use as the needed flag (initialized to false). Also declare your own event property you can use later.
Then override the OnDragDrop: There you set the flag to true and do a base OnDragDrop(...);
Then override the OnTextChanged: There you do a base OnTextChanged(...); and check the flag and if so raise that new event.
Now you can use this new rtb with a new event DragDrop completed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900