How do I get a control's location relative to its Form's location?

You need to convert to screen coordinates and then do some math.

Point controlLoc = form.PointToScreen(myControl.Location);

The form's location is already in screen coordinates.

Now:

Point relativeLoc = new Point(controlLoc.X - form.Location.X, controlLoc.Y - form.Location.Y);

That will give you the location relative to the form's upper-left corner, rather than relative to the form's client area.


I think that this will answer your question. Note that "this" is the form.

Rectangle screenCoordinates = control.Parent.ClientToScreen(control.ClientRectangle);
Rectangle formCoordinates = this.ScreenToClient(screenCoordinates);

It seems that the answer is that there is no direct way to do this.

(As I stated in the question I'm looking for a way other than using screen coordinates.)

Tags:

C#

Winforms