4
votes

I have TreeView, ListView and WebBrowser controls. I use the drag and drop method. With the TreeView and ListView everything works, because they have Dragdrop and DragEnter event. But I did not find them in WebBrowser.

I try using:

webBrowser1.Document.Body.Drag += new HtmlElementEventHandler(WebBrowser_Drag);
webBrowser1.Document.Body.DragOver += new HtmlElementEventHandler(WebBrowser_DragOver);
webBrowser1.Document.Body.DragEnd += new HtmlElementEventHandler(WebBrowser_DragEnd);
webBrowser1.Document.Body.DragLeave += new HtmlElementEventHandler(WebBrowser_DragLeave);

The DragOver and DragLeave events triggered, but it is not possible to change the cursor like

e.Effect = DragDropEffects.None;

The Drag and DragEnd events is not triggered.

I also try:

webBrowser1.Document.Body.AttachEventHandler("dragdrop", WebBrowser_DragDrop);
webBrowser1.Document.AttachEventHandler("dragdrop", WebBrowser_DragDrop);
webBrowser1.Document.AttachEventHandler("ondrop", WebBrowser_OnDrop);
webBrowser1.Document.Body.AttachEventHandler("ondrop", WebBrowser_OnDrop);

but it does not work well.

Now I have some questions:

  1. How to change the cursor in the event DragOver and DragLeave.
  2. Is there a way to process data as well as Dragdrop and DragEnter for WebBrowser?
1
They don't work at all like the normal desktop D+D events, you are supposed to manipulate the DOM yourself. Best starting point is to work from Javascript sample code. Try a google query like "javascript ondragenter getdata".Hans Passant

1 Answers

3
votes
  1. Changing the cursor here Change webbrowser Cursor. But among the possible cursors unfortunately no draganddrop cursor.
  2. The easiest way to solve my problem that I have found is to use a transparent panel Clearing the graphics of a transparent panel C#:

    private void WebBrowser_DragOver(object sender, HtmlElementEventArgs e)
    {
        panel.BringToFront();
    }
    
    private void Panel_DragLeave(object sender, EventArgs e)
    {
        panel.SendToBack();
    }
    
    private void Panel_MouseLeave(object sender, EventArgs e)
    {
        panel.SendToBack();
    }
    
    private void Panel_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }
    
    private void Panel_DragDrop(object sender, DragEventArgs e)
    {
        //Make dragdrop data processing
    }