Luke
Luke

Reputation: 11421

Drag and drop onto a form (but not a WebBrowser control on the form)

I'm writing a Windows Forms app (C#) that has a WebBrowser control (among a few other buttons and text boxes). I want to be able to drop a file anywhere on the form. The problem is that by default the WebBrowser will attempt to render any file dropped into its control; I don't want this as I have to perform some pre-processing on the file first. The WebBrowser control provides a property named AllowWebBrowserDrop, which I set to false to disable this behavior. However, the result is that I cannot drop anything on the WebBrowser control ("not allowed" feedback). The WebBrowser control takes up the majority of the space, so it would be kind of lame if you had to drop the file in the slack space somewhere. How can I enable dropping a file anywhere on my form without having the WebBrowser control try to render it?

I guess I should add that I have AllowDrop on my form set to true and have handlers for DragEnter and DragDrop. I have AllowWebBrowserDrop on my WebBrowser set to false. Everything else has the default settings.

Upvotes: 2

Views: 2411

Answers (2)

Jon Seigel
Jon Seigel

Reputation: 12401

Depending on what you're doing with the WebBrowser, you can handle the Navigating event, which fires before the browser navigates. Then determine if you want to handle the drop by checking the URL. For example:

private void browser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    if (e.Url.IsFile)
    {
        // Prevent navigation
        e.Cancel = true;

        // Fire your other OnDrop code
    }
}

For this to work, you would want to leave AllowWebBrowserDrop set to true.

If this works for your business case, great; otherwise you're probably stuck handling window messages directly, as mentioned, which isn't much fun.

Upvotes: 3

Uwe Keim
Uwe Keim

Reputation: 40736

Could you add a transparent background colored Panel with a higher z-order than the WebBrowser above it and let the Panel handle the drop?

Upvotes: 0

Related Questions