Dragging picture box from tree view on a panel not working properly
<p>Hello I am trying to drag and drop a picture box from a tree view onto a panel. My code is working somewhat but I have to click and drag at least twice in order for it to work. Also, I would like to be able to drag the picture box onto some random location on the panel and drop it off. At this point, once the mouse is over the panel it drops of the picture box.
Any help would be appreciated. Below is my code:   </p>

<pre><code>public partial class Form1 : Form
{
    List&lt;PictureBox&gt; pics = new List&lt;PictureBox&gt;();
    List&lt;string&gt; filenames = new List&lt;string&gt;();

    int x;
    int y;

    public Form1()
    {
        InitializeComponent();
        treeView1.Nodes.Add("IMG");
    }

    private void loadToolStripMenuItem_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog of = new OpenFileDialog())
        {
            of.ShowDialog();
            PictureBox pc = new PictureBox();
            pc.Load(of.FileName);
            pc.Size = new Size(pc.Image.Width, pc.Image.Height);
            pc.Name = pics.Count.ToString();
            pc.SizeMode = PictureBoxSizeMode.CenterImage;
            pics.Add(pc);
            pc.MouseDown += new MouseEventHandler(pc_MouseDown);
            pc.MouseMove += new MouseEventHandler(pc_MouseMove);
            filenames.Add(Path.GetFullPath(of.FileName));
            int index = (of.FileName).LastIndexOf("\\");
            string sub = (of.FileName).Substring(index + 1);
            treeView1.Nodes[0].Nodes.Add(sub);
        }
    }

    void pc_MouseMove(object sender, MouseEventArgs e)
    {
        PictureBox pc = (PictureBox)sender;
        if (e.Button == MouseButtons.Left)
        {
            pc.Left += (e.X - x);
            pc.Top += (e.Y - y);
        }
    }

    void pc_MouseDown(object sender, MouseEventArgs e)
    {
        PictureBox pc = (PictureBox)sender;
        if (e.Button == MouseButtons.Left)
        {
            x = e.X;
            y = e.Y;
        }
    }

    private void splitContainer1_Panel2_DragEnter(object sender, DragEventArgs e)
    {
        splitContainer1.Panel2.Controls.Add(pics[treeView1.SelectedNode.Index]);
        Point clientPoint = splitContainer1.Panel2.PointToClient(new Point(e.X, e.Y));
        splitContainer1.Panel2.Controls[treeView1.SelectedNode.Index].Location = clientPoint;
    }

    private void splitContainer1_Panel2_DragDrop(object sender, DragEventArgs e)
    {
        Point clientPoint = splitContainer1.Panel2.PointToClient(new Point(e.X, e.Y));
        splitContainer1.Panel2.Controls[treeView1.SelectedNode.Index].Location = clientPoint;
    }

    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
    }

    private void treeView1_MouseDown(object sender, MouseEventArgs e)
    {
        treeView1.DoDragDrop(pics[treeView1.SelectedNode.Index], DragDropEffects.Move);
    }

    private void splitContainer1_Panel2_MouseDown(object sender, MouseEventArgs e)
    {

    }

    private void treeView1_DragLeave(object sender, EventArgs e)
    {
    }
}
</code></pre>
