Windows Develop Bookmark and Share   
 index > Windows Forms General > Get x and y value of the drag and drop point without copy or move object
 

Get x and y value of the drag and drop point without copy or move object

hi developers
I have a DataGridView and picutureBox when I want to select an item (row) from DataGridView and then drag and drop in the pictureBox , I dont't want to copy or move an object from DataGridView to pictureBox .I just want to get the value x and y of the drag and drop point .
How can I do that .
thanks in advance

alladeen88  Monday, September 21, 2009 6:11 AM
That will take some doing, you first have to add the logic to start the D+D from the DGV. Add a DGV and a PB to a form. Add some columns to the DGV and change the BackColor of the PB. This code gets the job done:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
pictureBox1.AllowDrop = true;
pictureBox1.DragDrop += new DragEventHandler(pictureBox1_DragDrop);
pictureBox1.DragEnter += new DragEventHandler(pictureBox1_DragEnter);
dataGridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseDown);
dataGridView1.CellMouseUp += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseUp);
dataGridView1.CellMouseMove += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseMove);
}

Point cellPos;
bool cellDown;
Point dropPos;

void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
cellPos = e.Location;
cellDown = true;
}
void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) {
if (cellDown &&
(Math.Abs(e.X - cellPos.X) >= SystemInformation.DoubleClickSize.Width ||
Math.Abs(e.Y - cellPos.Y) >= SystemInformation.DoubleClickSize.Height))
{
if (dataGridView1.DoDragDrop("nobugz", DragDropEffects.Copy) == DragDropEffects.Copy)
MessageBox.Show("Dropped at " + dropPos.ToString());
cellDown = false;
}
}
void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) {
cellDown = false;
}

void pictureBox1_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Text) &&
(string)e.Data.GetData(DataFormats.Text) == "nobugz")
e.Effect = DragDropEffects.Copy;
}
void pictureBox1_DragDrop(object sender, DragEventArgs e) {
dropPos = pictureBox1.PointToClient(new Point(e.X, e.Y));
}
}


Hans Passant.
nobugz  Wednesday, September 23, 2009 1:07 AM

Hi alladeen88,

We can handle the MouseDown and MouseUp events of the DataGridView to get the drag and drop points when we drag from DataGridView and drop onto the PictureBox. This is the code snippet:

        private Point _dragPoint = Point.Empty; //Drag point.
        private Point _dropPoint = Point.Empty; //Drop point.
        private void Form3_Load(object sender, EventArgs e)
        {
            //Handle this event to get the drag point.
            this.dataGridView1.MouseDown += new MouseEventHandler(dataGridView1_MouseDown);
            //Handle this event to get the drop point.
            this.dataGridView1.MouseUp += new MouseEventHandler(dataGridView1_MouseUp);
        }
        void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            //Canculate the drag point.
            _dragPoint = this.PointToClient(Control.MousePosition);
        }
        void dataGridView1_MouseUp(object sender, MouseEventArgs e)
        {
            //Canculate the drop point.
            _dropPoint = this.PointToClient(Control.MousePosition);
            //If the drop position is not in the PictureBox, reset it.
            if (this.GetChildAtPoint(_dropPoint) != this.pictureBox1)
                _dropPoint = Point.Empty;
        }
        //Get the x, y of the drag and drop points and show them.
        private void ShowDragDropXY()
        {
            if (_dragPoint != Point.Empty && _dropPoint != Point.Empty)
            {
                int dragX = _dragPoint.X;
                int dragY = _dragPoint.Y;
                int dropX = _dropPoint.X;
                int dropY = _dropPoint.Y;
                MessageBox.Show(String.Format("drag x:{0}, drag y:{1}, drop x:{2}, drop y:{3}",
                    dragX, dragY, dropX, dropY));
            }
        }

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, September 22, 2009 9:47 AM
hi
thanks Aland for your assistance

I am sorry my previous question is not so clear , so I will change the question
I have a pictureBox and a button , and I want to begin drag from a button and then drop into a pictureBox , and I need the coordiante of the point while moving in the picutreBox and when the user making drop in PictureBox(( not the point when drag begining )).
When the user ends drag and drop I want to add some implementation ,but I don't need to move or copy any object from a button to a pictureBox

regards
Alladeen
alladeen88  Tuesday, September 22, 2009 10:58 PM
That will take some doing, you first have to add the logic to start the D+D from the DGV. Add a DGV and a PB to a form. Add some columns to the DGV and change the BackColor of the PB. This code gets the job done:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
pictureBox1.AllowDrop = true;
pictureBox1.DragDrop += new DragEventHandler(pictureBox1_DragDrop);
pictureBox1.DragEnter += new DragEventHandler(pictureBox1_DragEnter);
dataGridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseDown);
dataGridView1.CellMouseUp += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseUp);
dataGridView1.CellMouseMove += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseMove);
}

Point cellPos;
bool cellDown;
Point dropPos;

void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
cellPos = e.Location;
cellDown = true;
}
void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) {
if (cellDown &&
(Math.Abs(e.X - cellPos.X) >= SystemInformation.DoubleClickSize.Width ||
Math.Abs(e.Y - cellPos.Y) >= SystemInformation.DoubleClickSize.Height))
{
if (dataGridView1.DoDragDrop("nobugz", DragDropEffects.Copy) == DragDropEffects.Copy)
MessageBox.Show("Dropped at " + dropPos.ToString());
cellDown = false;
}
}
void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) {
cellDown = false;
}

void pictureBox1_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Text) &&
(string)e.Data.GetData(DataFormats.Text) == "nobugz")
e.Effect = DragDropEffects.Copy;
}
void pictureBox1_DragDrop(object sender, DragEventArgs e) {
dropPos = pictureBox1.PointToClient(new Point(e.X, e.Y));
}
}


Hans Passant.
nobugz  Wednesday, September 23, 2009 1:07 AM
hi all
thanks Hans Passant that is exactlywhat I look for

Regards
Alladeen
alladeen88  Wednesday, September 23, 2009 7:45 AM

You can use google to search for other answers

Custom Search

More Threads

• Skinning SCrollbars
• Cannot Updated!
• ToolStripControlHost DataGridView
• Multiple AutoComplete Sources
• Treeview question .net
• ToolStripMenuItem Activate property issue
• Am I running multiple instances??
• GUI design - Best practices
• Windows Forms: Iterating over widgets
• Introducing a struct in a second form