C# Get a control's position on a form

前端 未结 9 686
星月不相逢
星月不相逢 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:49

    private Point FindLocation(Control ctrl)
    {
        if (ctrl.Parent is Form)
            return ctrl.Location;
        else
        {
            Point p = FindLocation(ctrl.Parent);
            p.X += ctrl.Location.X;
            p.Y += ctrl.Location.Y;
            return p;
        }
    }
    

提交回复
热议问题