How to find the location of a control relative to it's containing form?
-
I'm developing a control derived from the ListView control and need it to find it's position relative to the Form it's contained on. In my test Form I have the ListView positioned on a TabPage, and all the normal positioning references (this.Bounds, this.Location, this.Top, this.Left, this.ClientRectangle etc) return 0 - which is it's correct position within the TabPage, but obviously has nothing to do with it's position on the parent Form. Note that I do not need a reference to it's Screen location, just the X and Y coordinates to it's upper-left corner within the parent form - can anyone help?
-
I'm developing a control derived from the ListView control and need it to find it's position relative to the Form it's contained on. In my test Form I have the ListView positioned on a TabPage, and all the normal positioning references (this.Bounds, this.Location, this.Top, this.Left, this.ClientRectangle etc) return 0 - which is it's correct position within the TabPage, but obviously has nothing to do with it's position on the parent Form. Note that I do not need a reference to it's Screen location, just the X and Y coordinates to it's upper-left corner within the parent form - can anyone help?
you need to offset the location by adding the parent location.
int x = Left; for (Control thisParent = parent; thisParent != null; thisParent = thisParent.parent) x += thisParent.Left;
"When the only tool you have is a hammer, a sore thumb you will have."
-
you need to offset the location by adding the parent location.
int x = Left; for (Control thisParent = parent; thisParent != null; thisParent = thisParent.parent) x += thisParent.Left;
"When the only tool you have is a hammer, a sore thumb you will have."
Many thanks for the quick reply! I tried implementing your suggestion as follows:
int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) x += thisParent.Left; int y = Top; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) y += thisParent.Top;
No go though I'm afraid - what that ends up giving me is a screen reference, not a refence relative to the containing form. Perhaps it has something to do with the fact the the test app is MDI? Even if so, I'd like to make a control that will work reliably in both SDI and MDI apps. -
Many thanks for the quick reply! I tried implementing your suggestion as follows:
int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) x += thisParent.Left; int y = Top; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) y += thisParent.Top;
No go though I'm afraid - what that ends up giving me is a screen reference, not a refence relative to the containing form. Perhaps it has something to do with the fact the the test app is MDI? Even if so, I'd like to make a control that will work reliably in both SDI and MDI apps.just stop one level earlier
int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) { if (this.Parent != null) x += thisParent.Left; }
"When the only tool you have is a hammer, a sore thumb you will have."
-
just stop one level earlier
int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) { if (this.Parent != null) x += thisParent.Left; }
"When the only tool you have is a hammer, a sore thumb you will have."
Thanks for the tips Philip - after stopping one level before the MDI parent I still had a problem where the MDI child form was reporting negative location values. The final code I came up with looks like this:
int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) { if (thisParent.Parent != null && thisParent.Left > 0) x += thisParent.Left; } int y = Top; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) { if (thisParent.Parent != null && thisParent.Top > 0) y += thisParent.Top; }