如何相对于父对象定位对象

How to position objects relative to their parent

我想知道是否可以相对于父面板定位面板。 例如,我有一个面板,每隔一段时间就会收到一个子面板,但不是这样做:
newChildPanel.Location = Point.Add(parentPanel.Location,desiredLocation);

我想说:

newChildPanel.Location = Size.new(0,64);

有什么办法吗?

newChildPanel.Location = 新点(0, 64);

您可以通过定义一个新点来设置位置:newChildPanel.Location = new Point(0, 64)。它将相对于 parent 的位置。

例如:

Panel parent = new Panel();
parent.BackColor = Color.Red;
parent.Location = new Point(20, 25);
Controls.Add(parent);

Panel child = new Panel();
child.Parent = parent;
child.BackColor = Color.Blue;
child.Location = new Point(5, 5);

给出以下结果:

如您所见,child 面板(蓝色)已添加到相对于 parent 的(红色)位置的 (5, 5)。