Never been to DZone Snippets before?

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

Calculate control's absolute screen location (See related posts)

For example for control Label1:

this.PointToScreen(this.Label1.Location)

Comments on this post

MrBurns posts on Jan 11, 2008 at 02:13
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;
        }

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts