Click to See Complete Forum and Search --> : RectangleToScreen please explain.


Rigel
August 1st, 2008, 05:09 PM
I'm using RectangleToScreen to try to find the exact screen coordinates of a control in a form.


Rectangle rectQD = m_qd.Control.FindForm().RectangleToScreen(m_qd.Control.Bounds);


This piece of code above sometimes works. I've seen this sometimes return the screen location of the parent form not the location of the control residing in it. ???


Rectangle rectQD = m_qd.Control.RectangleToScreen(m_qd.Control.FindForm().ClientRectangle);


This piece of code above always returns the correct screen location of the control. But it doesn't make any sense to me. Can someone explain?

Thank you,

BigEd781
August 1st, 2008, 06:58 PM
control.x and control.y will give you the screen coordinates of a control relative to the parent form.

MadHatter
August 1st, 2008, 08:40 PM
Control has a property called Parent. If you drop a control on to a form, its parent will be the form. if you drop a control inside a panel inside the form, then that control's parent will be the container its nested inside. FindForm gets the control that is the containing form for the control (it walks the Parent tree for you).

like biged said, the control's coords are relative to its container, so if you have a button sitting inside the form at x=7, y=7, and call FindForm().PointToScreen(control.Location), its going to take the offset 7,7 and add the forms location (relative to the screen) and return that location.

if your control has a panel on it (at 7,7) and a button inside it (at 7,7) and you call its RectangleToScreen, its going to recursively obtain correct offset info back to the screen (control's location of 7,7 + container's location of 7,7, plus form's location of n,n). calling FindForm and passing in a point or rect into it, is going to skip any controls it may be nested inside, and could end up giving you the wrong point as an offset.