C#仅在一个大面板中创建面板

C#仅在一个大面板中创建面板,c#,winforms,panel,C#,Winforms,Panel,C#仅在一个大面板中创建面板 我有以下代码(框大小随机生成): //设置面板的位置 int xbox=r.Next(10766); int ybox=r.Next(10390); if(ybox+box.Height>=boxPanel.Location.Y+boxPanel.Height) { ybox=ybox-100; } else if(xbox+box.Width>=boxPanel.Location.X+boxPanel.Width) { xbox=xbox-100; } else-

C#仅在一个大面板中创建面板 我有以下代码(框大小随机生成):

//设置面板的位置
int xbox=r.Next(10766);
int ybox=r.Next(10390);
if(ybox+box.Height>=boxPanel.Location.Y+boxPanel.Height)
{
ybox=ybox-100;
}
else if(xbox+box.Width>=boxPanel.Location.X+boxPanel.Width)
{
xbox=xbox-100;
}

else-if(xbox-box.Width这些语句上不应该有
else
;它们都应该是独立的
if
语句。如前所述,如果触发第一个
y
检查,则不会运行任何其他块/检查

为什么不首先在正确的范围内随机生成
x
y

例如:

// assumes "box" was already created with a desired width/height
int xbox = r.Next(10, boxPanel.Width - box.Width - 10);
int ybox = r.Next(10, boxPanel.Height - box.Height - 10);
box.Location = new Point(xbox, ybox);
boxPanel.Controls.Add(box);

这将使框在另一个面板中,边缘周围有10个像素的“边缘”。

你的问题到底是什么?无论如何,你可能想考虑使用A。你可能试图把你的<代码>框< /COD>面板添加到一个父代码> Box Panel 。所以,你缺少了“代码> Box Panel。控件。当您定义子面板的位置时,不要考虑容器的位置。如果您只需要随机大小和子控件的位置不相关,则使用FlowLayoutPanel作为所提到的。否则,只需随机地定义它们的位置/大小并将它们添加到容器中。<代码> BruttoFrON()
可用于在顶部显示添加的最后一个子控件。