mouse coordinates relative to a control

R

Robin Senior

Hi,
I'm trying to drag and drop onto a Panel on my form. The panel is inside
a groupBox, which of course is inside my form.

When dropping the item onto my Panel, I want it to appear at that point
on the Panel, and therefore need the mouse coordinates relative to the
Panel and not the form itself. Something like:

private void myPanel_DragDrop(object sender, DragEventArgs e)
{
tempPoint = //insert location finding code here...
draggedLabe.Text = (string) e.Data.GetData(DataFormats.Text);
draggedLabel.Location = tempPoint;
myPanel.Controls.Add(draggedLabel);
}

I've tried doing this by taking the DragEventArgs mouse coords and
subtracting the panel's location and the groupBox's location, but still
no dice.

I've also tried using:
(Point) (visPanel.Location + (Size) (PointToScreen(Point.Empty) - (Size)
Location));

Which should give the mouse coords relative to my control, but it
doesn't refresh for some reason.

Any simple solutions? I've googled for ages...

Cheers,
-robin

reverse my username to email me.
 
N

n!

When dropping the item onto my Panel, I want it to appear at that point
on the Panel, and therefore need the mouse coordinates relative to the
Panel and not the form itself. Something like:

Do you have a reference to the form which is the source of the Drag
operation? I assume that the 'sender' parameter you are receiving is one?

private void myPanel_DragDrop( object sender, DrawEventArgs e )
{
Control source = ( Control )sender;

// Obtain screen location of drag event...
Point screen = source.PointToScreen( new Point( e.X, e.Y ) );

// And convert it to our local coordinate system...
Point local = PointToClient( screen );
}

Which should leave you with a location local to your panel inside the
'local' variable.

n!
 
R

Robin Senior

n! said:
Do you have a reference to the form which is the source of the Drag
operation? I assume that the 'sender' parameter you are receiving is one?

private void myPanel_DragDrop( object sender, DrawEventArgs e )
{
Control source = ( Control )sender;

// Obtain screen location of drag event...
Point screen = source.PointToScreen( new Point( e.X, e.Y ) );

// And convert it to our local coordinate system...
Point local = PointToClient( screen );
}

Which should leave you with a location local to your panel inside the
'local' variable.

Hi,
You almost got it, but still lead me in the right direction. All I
needed to do was:
Point local = myPanel.PointToClient( new Point(e.X,e.Y));

Thanks so much!
-robin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top