DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Calculate Control's Absolute Screen Location

04.27.2007
| 892 views |
  • submit to reddit
        For example for control Label1:

this.PointToScreen(this.Label1.Location)
    

Comments

Snippets Manager replied on Thu, 2008/01/10 - 5:35am

I don't think this works, at least not if the control is inside another control (Panel/groupbox/whatever). This one works for me: private Point findCoords2(Control c) { // Recurse through all parent controls... Point coord = new Point(); // Own location inside control coord = c.Location; // Add parents control coord (There is always at least one parent! ... if this isn't the Form...) while (c.Parent != null) { c = c.Parent; coord.X += c.Location.X; coord.Y += c.Location.Y; } // Add pixel borders coord.X += c.Margin.Left + 1; // 23 for "classic" Windows look // 30 for Windows XP standard look //coord.Y += 30; // Or try this... coord.Y += c.Bounds.Height - c.ClientSize.Height - (c.Margin.Top + 1); return coord; }