C# Get a control's position on a form

前端 未结 9 683
星月不相逢
星月不相逢 2020-11-27 12:10

Is there any way to retrieve a control\'s position in a form, when the control may be inside other controls (like Panels)?

The control\'s Left and Top properties giv

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 12:43

    Supergeek, your non recursive function did not producte the correct result, but mine does. I believe yours does one too many additions.

    private Point LocationOnClient(Control c)
    {
       Point retval = new Point(0, 0);
       for (; c.Parent != null; c = c.Parent)
       { retval.Offset(c.Location); }
       return retval;
    }
    

提交回复
热议问题