0

I'm trying to drag and drop some random selected text from a random webpage within Firefox onto a textbox in my Winform application, but for some reason I can't get it to work. I have set AllowDrop to true on the control(textbox) and I'm handling the events DragEnter and DragDrop, so that's not the problem. Anyone know what the problem could be?

My code looks like this:

    public Form1()
    {
        InitializeComponent();

        tbISBN.DragDrop += new DragEventHandler(tbISBN_DragDrop);
        tbISBN.DragEnter += new DragEventHandler(tbISBN_DragEnter);
        tbISBN.AllowDrop = true;           
    }

    void tbISBN_DragEnter(object sender, DragEventArgs e)
    {
        foreach (var param in e.Data.GetFormats())
            Console.WriteLine(param);

        if ((e.AllowedEffect & DragDropEffects.All) != 0 && e.Data.GetDataPresent(typeof(string)))
        {
            e.Effect = DragDropEffects.All;
        }
    }

    void tbISBN_DragDrop(object sender, DragEventArgs e)
    {
        string stringData = e.Data.GetData(typeof(string)) as string;
        MessageBox.Show(stringData);
    }

4 Answers4

0

This should get you started

    public Form1()
    {
        InitializeComponent();
        AllowDrop = true;
        DragEnter += new DragEventHandler(Form1_DragEnter);
        DragDrop += new DragEventHandler(Form1_DragDrop);
    }

    void Form1_DragEnter(object sender, DragEventArgs e)
    {
        if ((e.AllowedEffect & DragDropEffects.All) != 0 && e.Data.GetDataPresent(typeof(string)))
        {
            e.Effect = DragDropEffects.All;
        }
    }

    void Form1_DragDrop(object sender, DragEventArgs e)
    {
        string stringData = e.Data.GetData(typeof(string)) as string;
        MessageBox.Show(stringData);
    }
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • I tried your example but still no luck! If I select some text from Visual Studios console/output window, then I can drag/drop it in the textbox, but I still can't drag/drop text from Firefox! – CindytheTiger Nov 30 '12 at 12:24
0

For DragEnter and DragDrop you should tell which data type you want to process. In case of text from Firefox you should use StringFormat if you want the text string itself. Text is also possible, but not that flexible.

This works for all plain text in Firefox. When you copy text which belongs to a link, then you will receive the hyperlink target instead.

void MainFormDragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.StringFormat)) 
        e.Effect = DragDropEffects.Copy;
}

void MainFormDragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.StringFormat)) {
        string dropText = (string)e.Data.GetData(DataFormats.StringFormat);
        Debug.WriteLine(dropText);
    }

}
0

On Windows 7 it may not work when you run Visual Studio as Administrator while Firefox runs with lower privileges. See this answer: C# Drag drop does not work on windows 7

Running the program outside Visual Studio will do the trick.

Community
  • 1
  • 1
Wintermute
  • 394
  • 4
  • 19
0

Use e.Data.GetData(DataFormats.Html) to retrieve the text and Source URL or e.Data.GetData(DataFormats.Text for only the text.

EuroMarkus
  • 31
  • 1
  • 3