Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Drag and Drop from Internet Explorer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    [RESOLVED] [2.0] Drag and Drop from Internet Explorer

    Does anyone know how to drag and drop links from IE into a C# application? I am able to capture data from Mozilla Firefox without problems using this code:
    Code:
    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            // Firefox sends the url on the first line, the link text on the 2nd
            string url = e.Data.GetData(DataFormats.StringFormat).ToString();
            if (url.Contains("\n")) url = url.Substring(0, url.IndexOf("\n"));
            textBox1.Text = url;
        }
    }
    
    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            e.Effect = DragDropEffects.Copy;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }
    This same code does not work with Internet Explorer. I have tried IE6 and IE7 with the same results. I have done this before in VB, using the following code:
    Code:
    Private Sub lstMain_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstMain.DragOver
        If e.Data.GetData("System.String").ToString().StartsWith("http://") Then e.Effect = DragDropEffects.Link
    End Sub
    
    Private Sub lstMain_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstMain.DragDrop
    
    Dim SiteName As String = "", SiteURL As String = "http://", SiteDescription As String = "", SiteFile As String
    Dim temp() As String, temp2 As String
    temp2 = e.Data.GetData("System.String")
    If temp2.Contains(vbLf) Then
        temp = Split(temp2, vbLf)
        SiteURL = temp(0)
        SiteName = temp(1)
    Else
        SiteURL = temp2
        SiteName = temp2.Substring(temp2.LastIndexOf("/") + 1)
    End If
    That code works fine in my VB application, but when checking for anything in the e.Data.GetDataPresent() method via the IE drop, nothing happens. I have tried all of the even remotely logical options in the DataFormats class, as well as passing in my own strings like "System.String" and "UniformResourceLocator". No matter what I try, dragging from IE doesn't trigger the code that changes the drag effect to Copy. If I put a break point on the code, it does enter the if statement, but the cursor never changes, and the DragDrop event never fires, not even the first if statement.

    Please help and thank you!

    PS, what happened to the [vbcode] tags? Clicking the VBCode button just asks me for highlighted text, which then turns red.
    Last edited by supertotallyawesome; Jun 28th, 2008 at 02:43 PM. Reason: Resolved

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: [2.0] Drag and Drop from Internet Explorer

    Call GetDataFormats to determine what formats the data being dropped is available in. You can then get the data in the most appropriate of the available formats and process it accordingly.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    Re: [2.0] Drag and Drop from Internet Explorer

    Quote Originally Posted by jmcilhinney
    Call GetDataFormats to determine what formats the data being dropped is available in. You can then get the data in the most appropriate of the available formats and process it accordingly.
    I have also tried that. Here is the code I tried using the GetFormats method:
    Code:
                foreach (string s in e.Data.GetFormats())
                {
                    if (s.Contains("UniformResourceLocator"))
                    {
                        e.Effect = DragDropEffects.Copy;
                        break;
                    }
                }
    The e.Effect = DragDropEffects.Copy line does triggers and if I put a breakline on the "break" keyword, and check the value of e.Effect, it is set to Copy. The problem is the cursor never does change to the copy cursor, and the DragDrop method never fires properly. I even went to so far as to just do this:
    Code:
    if(true)
    {
        e.Effect = DragDropEffects.Copy;
    }
    and the cursor still does not change! I don't even check the format, I just say change the cursor, and it doesn't work from IE. Anything else I drag onto the window works, just not any link from IE.

    I thought maybe it was a security issue, since I'm running thin on ideas. In the IE security settings, I found an option called "Drag and drop or copy and paste files" but it is enabled. Nothing else in there seems to relate to anything I am doing though. Just for sh*ts and giggles, I turned my security settings as low as they would go in IE7 for the internet zone and it still did not work.

    I just now found out that if I highlight regular text on a webpage in IE, and drag that to my window, it will trigger and drop the text properly, but that's useless, since I only want to allow people to drop links. I will have to check for a protocol in the string to make sure it's a valid URL.

    So any other ideas would be most appreciated. Maybe I am just missing some simple property...

    Thanks

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    Re: [2.0] Drag and Drop from Internet Explorer

    d'oh. I just figured it out. I realized there is an AllowedEffects property available. I checked that, and although firefox will allow both Copy and Link, Internet Explorer will only allow Link. As soon as I changed it to Link, it started working. Man that was a stupidly obvious thing to overlook, in hindsight.

    Now that I think about it, my VB code that worked WAS using Link instead of Copy... *sigh*

    Thanks j, you got me thinking about all those other properties. Here is the code that works:
    Code:
    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            string url = e.Data.GetData(DataFormats.StringFormat).ToString();
            if (url.Contains("\n")) url = url.Substring(0, url.IndexOf("\n"));
            if (!url.Contains("http://")) url = String.Empty;
            textBox1.Text = url;
           }
    }
    
    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Text) || e.Data.GetDataPresent("UniformResourceLocator"))
        {
            e.Effect = DragDropEffects.Link;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }
    Last edited by supertotallyawesome; Jun 28th, 2008 at 02:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width