简体   繁体   中英

How to position objects relative to their parent

I am wondering if it is possible to position a panel relative to it's parent. For example, I have a panel that every so often will receive a child panel, but instead of doing:
newChildPanel.Location = Point.Add(parentPanel.Location,desiredLocation);

I want to say:

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

Is there any way to do this?

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

You can set the location by defining a new point: newChildPanel.Location = new Point(0, 64) . It will be relative to the parent's position.

For example:

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);

gives the following result:

在此处输入图片说明

As you can see, the child panel (blue) was added at (5, 5) relative to the parent's (red) position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM