467,146 Members | 1,230 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

mouse coordinates relative to a control

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.
Nov 15 '05 #1
  • viewed: 20595
Share:
2 Replies
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!
Nov 15 '05 #2
n! wrote:
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
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Danny@Kendal | last post: by
2 posts views Thread by Robert Chan | last post: by
8 posts views Thread by Tom | last post: by
2 posts views Thread by quickcur@yahoo.com | last post: by
4 posts views Thread by Johnny Jensen | last post: by
4 posts views Thread by =?Utf-8?B?Unlhbg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.