面板之间的静态距离

本文关键字:静态 距离 之间 | 更新日期: 2023-09-27 18:30:22

>我有一个动态创建的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

面板之间的静态距离

如果你想

让它变得简单,当你添加那些面板时,我想这是动态完成的。您可以设置 panel.tag = "[订单]"。所以分配一个订单号,这样你就知道哪个是第一个在顶部,第二个是第二个,然后继续......

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

这里有一个快速完成的示例项目下载 这里

编辑:更新的链接

可能值得一看"流布局面板"。将控件添加到流程布局面板时,该控件将自动定位到该流程布局面板中任何现有控件的左侧、右侧、顶部或底部(具体取决于您指定的布局方向属性)。

同样,如果删除流布局面板中包含的控件或调整其大小,控件的位置将自动更新。

在您的情况下,它应该像向表单添加新的流布局面板一样简单,然后,对于添加到流布局面板的每个面板,将上边距设置为 15px,将下边距设置为 0px;流布局面板将负责其余的工作。添加、删除面板或调整面板大小时,流程布局面板将确保它们仍然正确显示,它们之间的边距为 15px。