DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: C# Drag and Drop fail  (Read 10,009 times)

mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 247
C# Drag and Drop fail
« on: November 11, 2008, 06:30 PM »
Hi all, I'm doing really badly on my first try at a drag and drop. I'm trying to have a listBox that can have a file dragged onto it from Windows Explorer

From the very simple example here - http://support.microsoft.com/kb/307966 I have

Set AllowDrop on the list to TRUE
Set the event DragDrop
Set the event DragEnter

I have the following code

Code: (csharp)
private void listFilesToConvert_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            // DEBUG
            MessageBox.Show("DragEnter");

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.All;
            else
                e.Effect = DragDropEffects.None;
        }

        private void listFilesToConvert_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            // DEBUG
            MessageBox.Show("DragDrop");
            
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop);
            int i;
            for (i = 0; i < s.Length; i++)
                listFilesToConvert.Items.Add(s[i]);

        }

I can't say I understand it all, but it looks very similar to other code on the net. It's probably something simple but I can't work it out.

Neither debug message boxes fire.

Cheers

Graham R
Learning C# - Graham Robinson

fenixproductions

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,186
    • Participated in NANY Challenge Four Times
Re: C# Drag and Drop fail
« Reply #1 on: November 11, 2008, 07:56 PM »
2mediaguycouk
Works perfect here. Well... "almost perfect". Messago box has to be clicked and when I do so, dragged files are lost.

Maybe the reason is related to this somehow?

Use: Debug.WriteLine() for debugging and check will there be anything in Output panel.

PS. Check is it working inside your Visual Studio:
http://fenixproductions.republika.pl/tmp/test_drop.rar
Надо было учиться, а не камни в школу бросать...
--
f0dder is my personal hero :)
« Last Edit: November 11, 2008, 08:00 PM by fenixproductions »

mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 247
Re: C# Drag and Drop fail
« Reply #2 on: November 11, 2008, 08:00 PM »
I shall thank Nutcase - http://channel9.msdn.com/forums/Coffeehouse/254293-c-Dragging-and-Dropping/

Visual Studio 2005 on Vista needs to run as administrator, explorer runs as a normal user. Vista doesn't allow programs using different privileges to interact.

So my code has worked for 3 hours :'(

(And thanks for the debug command, I didn't know it existed)
Learning C# - Graham Robinson

Original address: https://www.donationcoder.com/forum/index.php?topic=15687.0