我有一个动态创建的N个面板,它们之间的距离高度为15px
panel.Location = new Point (x, y);
y = panel.Bottom + 15;
我可以制作较小的宽度,因此我需要面板之间的高度距离始终为15px 我有一个方法用于调整大小的不同检查,我尝试更改距离,但它总是以不同的方式...
public void checkResize(string msg_out, object panel_sender, object text_msg_sender, int panHei, int numbs)
{
Panel pan_item = (Panel)panel_sender;
Label lab_item = (Label)text_msg_sender;
char[] msg_arr = msg_out.ToCharArray();
int panWidRaz = 308 - pan_item.Width;
int panWidw = pan_item.Width;
if (int.Parse(pan_item.Name) != numbs - 1)
{
if (panWidw < buff)
{
/* if (panWidRaz % 15 == 0)
{
for (int i = int.Parse(pan_item.Name); i >= 0; i--)
{
panel1.Controls[i.ToString()].Location = new Point(panel1.Controls[i.ToString()].Location.X, panel1.Controls[i.ToString()].Location.Y + 1);
}
}*/
//width control becomes smaller panels are becoming more in height, it is necessary that the distance between the panels remained 15px
}
if (panWidw > buff)
{
/*if (panWidRaz % 15 == 0)
{
for (int i = int.Parse(pan_item.Name); i >= 0; i--)
{
panel1.Controls[i.ToString()].Location = new Point(panel1.Controls[i.ToString()].Location.X, panel1.Controls[i.ToString()].Location.Y - 1);
}
}*/
//width control becomes bigger panels are becoming less in height, it is necessary that the distance between the panels remained 15px
}
buffCountPan++;
if (buffCountPan == panel1.Controls.Count - 1)
{
buff = panWidw;
buffCountPan = 0;
}
if (msg_arr.Length > 26)
{
int panWid = (308 - pan_item.Width) / 5;
int panWidLab = 308 - pan_item.Width;
pan_item.Height = panHei + panWid;
lab_item.MaximumSize = new System.Drawing.Size(300 - panWidLab, 100);
lab_item.MinimumSize = new System.Drawing.Size(300 - panWidLab, 14);
}
}
}
我不能在这里张贴图片......声誉......我让我的小组工作变得紧张 http://pixs.ru/showimage/Bezimeni1p_9639414_8969341.png
答案 0 :(得分:0)
如果你想简单,当你添加那些我认为动态完成的面板时。您可以设置panel.tag =“[Order]”。所以一个订单号分配,所以你知道哪一个在第一个,第二个,然后继续......
Const int MinimumStartLocation = 0;
Const int DistanceBetweenPanel = 15;
private void setPanelLocation(Panel pnl)
{
// retreive the order of this panel
int iPanelOrder = Convert.ToInt32(pnl.Tag);
// the first panel must show on top and have specific location
if(iPanelOrder == 0)
{
pnl.Top = MinimumStartLocation;
}
else
{
// set the top of the panel to the bottom value of the panel just before him in the order plus the constant
pnl.Top = this.Controls.OfType<Panel>().ToList().Find(pan => Convert.ToInt32(pan.Tag) == iPanelOrder -1).Bottom + DistanceBetweenPanel;
}
}
现在使用这个LINQ命令,它将以正确的顺序为每个面板调用上面的方法。
this.Controls.OfType<Panel>().OrderBy(x => Convert.ToInt32(x.Tag)).ToList().ForEach(pan => setPanelLocation(pan));
这里有一个快速完成的示例项目download here
修改:更新后的链接
答案 1 :(得分:0)
值得一看Flow Layout Panel。将控件添加到流布局面板时,它将自动定位到该流布局面板中任何现有控件的左侧,右侧,顶部或底部(取决于您指定的布局方向属性)。
同样,如果删除或调整流程布局面板中包含的控件的大小,控件的位置将自动更新。
在您的情况下,它应该像在表单中添加新的流布局面板一样简单,然后,对于添加到流布局面板的每个面板,将上边距设置为15px,将下边距设置为0px;流程布局面板将负责其余部分。当添加,删除或调整面板大小时,流程布局面板将确保它们仍然正确显示,它们之间的边距为15px。