1

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

4

2 回答 2

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

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

编辑:更新的链接

于 2013-09-06T12:15:52.343 回答
0

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

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

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

于 2013-09-06T14:05:07.123 回答