How can we get the location relative to window form?
.net, c#, drag-and-drop
Solution
You can get cursor coordinates using `Cursor.Position`, this gets you screen coordinates. you can then pass these into `PointToClient(Point p)`
Point screenCoords = Cursor.Position;
Point controlRelatedCoords = this.panel1.PointToClient(screenCoords);
Though, I am fairly certain that `DragEventArgs.X` and `DragEventArgs.Y` are already screen coordinates. Your problem probably lies in
if (e.X < 429 && e.X > 0 && e.Y<430 && e.Y>0)
This looks like it would be checking against Panel coordinates, whereas `e.X` and `e.Y` are screen coordinates at that point. Instead, thansform it into panel coords before checking against bounds :
Point screenCoords = Cursor.Position;
Point controlRelatedCoords = this.panel1.PointToClient(screenCoords);
if (controlRelatedCoords.X < 429 && controlRelatedCoords.X > 0 &&
controlRelatedCoords.Y < 430 && controlRelatedCoords.Y > 0)
{
}
Problem
I am implementing an application which can be drag and drop images in a panel and so I want to make sure that the image is placed within the panel and it is visible the whole image when is dropped.In that case I want to get the current cursor position when I doing drag and drop event. So how can I get the cursor location related to panel? Here is the method of panel dragdrop event. ``` private void panel1_DragDrop(object sender, DragEventArgs e) { Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control; if (c != null) { if (e.X < 429 && e.X > 0 && e.Y<430 && e.Y>0) { c.Location = this.panel1.PointToClient((new Point(e.X, e.Y)));** this.panel1.Controls.Add(c); } } } ```