views:

1260

answers:

2

I want to drag controls on panel and when dragging I want to move the control and get its location to drop on to panel.I have tried out mouseUp,mouseDown,MouseMove events of control.But that is not what I am looking for.I want to fire DragDrop event on panel and move control.Can I do this?If you can give me an idea it will be great.Below is part of my code.Please correct me.Thanks a lot.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace DragnDrop

{

public partial class Form1 : Form

{

    public Form1()
    {
        InitializeComponent();
    }
    Control mycontrol;
    int x, y;
    //Form1 f = new Form1();
    private void Form1_Load(object sender, EventArgs e)
    {


        foreach (Control c in this.panel1.Controls)
        {
            c.MouseMove += new MouseEventHandler(lblDragger_MouseMove);
            c.MouseUp += new MouseEventHandler(lblDragger_MouseUp);
            c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            c.MouseDoubleClick += new MouseEventHandler(pictureBox1_MouseDown);
        }

       panel2.AllowDrop = true;
       foreach (Control c in this.panel2.Controls)
       {
           c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);

       }
        panel2.DragOver += new DragEventHandler(panel2_DragOver);
        panel2.DragDrop += new DragEventHandler(panel2_DragDrop);  
    }
   bool isDragging ;
      int  clickOffsetX ;
      int  clickOffsetY ;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {

      //  this.Cursor = Cursors.SizeAll;
        //pictureBox1 = (PictureBox)sender;
        Control c = sender as Control;

        //DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
       // validation = true;
        isDragging = true;
        clickOffsetX = e.X;
        clickOffsetY = e.Y;
      //  c.DoDragDrop(c, DragDropEffects.Move);  
    }
    private void lblDragger_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e)
    {
        isDragging = false;
    }




    private void panel2_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(Bitmap)))
        {
            e.Effect = DragDropEffects.Copy;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }

    private void panel2_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;  
    }

    private void panel2_DragDrop(object sender, DragEventArgs e)
    {
        Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
        mycontrol = c;
        if (c != null)
        {
            c.Location = this.panel2.PointToClient(new Point(e.X, e.Y));
            this.panel2.Controls.Add(c);
        }  
    }


    private void lblDragger_MouseMove(System.Object sender,
      System.Windows.Forms.MouseEventArgs e)
    {
        Control c = sender as Control;
       // bool isDragging = true;
        if (isDragging == true)
        {
            c.Left = e.X + c.Left - clickOffsetX;
            c.Top = e.Y + c.Top - clickOffsetY;
        }
    }


 private void panel1_MouseLeave(object sender, EventArgs e)
    {
        Control c = sender as Control;

        c.DoDragDrop(c, DragDropEffects.Move); 
    }


}

}

+1  A: 

If your control is already on the panel and you're simply moving it within the same panel, then using the Mouse events is probably the easiest way to do this. My understanding is that Drag and Drop is more about conveying data between controls or even applications. Drag and drop would be a good fit if you're trying to allow a control to transfer between panels, for example.


If you want to do both, then here's one possible idea:

  1. Perform move dragging within the same panel using your Mouse events.

  2. When you get a MouseLeave event on the panel, begin a DragDrop operation (some examples here) You can either remove the control from the panel or add some sort of 'gray out' effect to indicate that the control may be leaving.

  3. Handle the DragDrop on your target panel and place the control at the mouse location of the drop.

This combines the intuitive feel of dragging the control around, while also providing a way to drag 'past' the panel and on to a new surface.

Dan Bryant
Thank you for your response.yes, I want to move controls within panel and also want to drag and drop controls from one panel to another panel.So I want to achieve both.How can I do that?
C. Karunarathne
Thank you.Here you are saying to handle c.DoDragDrop(c, DragDropEffects.Move); at mouseLeave event of first panel?Am I correct?But it is doing only the moving of controls within same panel. :(
C. Karunarathne
You need to do the following (there are lots of examples of DragDrop in general to be found on Google):- Call DoDragDrop when the mouse leaves, using the control you've been 'dragging' inside the panel- Make sure your target panel has AllowDrop set to true- Handle the DragEnter and DragDrop events of your target panel to provide context and handle the actual dropping operation.
Dan Bryant
But when I call DoDragDrop when mouse leave there cannot be drag controls to other panel.I have handled dragdrop, DragEnter events in the second panel also.ButtThey only move on the same panel.What have missed up?
C. Karunarathne
I have added a part of my code also.I wish you can help me.Thank you
C. Karunarathne
The main thing I noticed is that you try to add the control to the second panel before removing it from the first panel. Try removing it from the first panel. If that's not working, use the debugger to make sure panel2_DragDrop is being called with the control you expect.
Dan Bryant
Thank you for your response and very sorry for troubling.There what is happening is when moving the control the mouse cannot leave the panel with control.The control is only moving within the panel with mouse pointer.It cannot be dragged to the other.
C. Karunarathne
Is there any method to doing this as this is an urgent issue to solve for completion of my project?please
C. Karunarathne
A: 

You will need to use the mouse up and mouse down events to toggle your drag state. When you go mouse down, you start dragging. You record the relative position of the mouse within your control and the relative position of your control within the panel. You then follow the mouse as it moves and repositioning the control's top and left relative to the original location of the mouse within your control.

Payton Byrd