簡體   English   中英

如何相對於父對象放置對象

[英]How to position objects relative to their parent

我想知道是否有可能相對於其父面板定位面板。 例如,我有一個小組,通常每隔一個小組會收到一個子小組,但沒有這樣做:
newChildPanel.Location = Point.Add(parentPanel.Location,desiredLocation);

我想說:

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

有什么辦法嗎?

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

您可以通過定義新點來設置位置: newChildPanel.Location = new Point(0, 64) 這將相對於父母的位置。

例如:

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

給出以下結果:

在此處輸入圖片說明

如您所見,子面板(藍色)相對於父面板(紅色)的位置被添加在(5,5)處。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM