Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

14. Windows Forms Patterns

14.4 How to get the hyperlink and the hyperlink text dragged from IE in my Control's drag-drop event?


Compiled from the newsgroup posts by Andy Fish and Brian:

You can do this in the DragDrop event handler of your control:


[C#]
try
{
     //Use e.Data.GetData("UniformResourceLocator") to get the URL
     System.IO.Stream ioStream=
     (System.IO.Stream)e.Data.GetData("FileGroupDescriptor");
     byte[] contents = new Byte[512];
     ioStream.Read(contents,0,512);
     ioStream.Close();
     StringBuilder sb = new StringBuilder();
     //The magic number 76 is the size of that part of the
     //FILEGROUPDESCRIPTOR structure before the filename starts - cribbed
     //from another usenet post.
     for (int i=76; contents[i] != 0; i++)
     {
          sb.Append((char)contents[i]);
     }
     if (!sb.ToString(sb.Length-4,4).Equals(".url"))
     {
          throw new Exception("filename does not end in '.url'");
     }
     filename = sb.ToString(0,sb.Length-4);

}
catch(Exception ex)
{
     MessageBox.Show(ex.ToString());
}



[VB.Net]
Try
     'Use e.Data.GetData("UniformResourceLocator") to get the URL
     System.IO.Stream ioStream=
     CType(e.Data.GetData("FileGroupDescriptor"), System.IO.Stream)
     Dim contents() As Byte = New Byte(512) {}
     ioStream.Read(contents,0,512)
     ioStream.Close()
     Dim sb As StringBuilder = New StringBuilder()
     
     'The magic number 76 is the size of that part of the
     'FILEGROUPDESCRIPTOR structure before the filename starts - cribbed
     'from another usenet post.
     Dim i As Integer
     For i = 76 To contents(i) 0 Step i + 1
          sb.Append(CType(contents(i), Char))
     Next
     If Not sb.ToString(sb.Length-4,4).Equals(".url") Then
          Throw New Exception("filename does not end in '.url'")
     End If
     filename = sb.ToString(0,sb.Length-4)
Catch ex As Exception
     MessageBox.Show(ex.ToString())
End Try