[英]static distance between the panels
我有一個動態創建的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 =“ [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));
在此示例項目迅速完成此處下載
編輯:更新的鏈接
也許值得一看“ 流程布局面板” 。 將控件添加到流布局面板時,它將自動定位到該流布局面板中任何現有控件的左側,右側,頂部或底部(取決於您指定的布局方向屬性)。
同樣,如果您刪除或調整流布局面板中包含的控件的大小,控件的位置將自動為您更新。
在您的情況下,這就像將一個新的流程布局面板添加到表單一樣簡單,然后,對於您添加到流程布局面板的每個面板,將頂部邊距設置為15px,將底部邊距設置為0px; 流程布局面板將負責其余的工作。 當添加,刪除面板或調整面板大小時,流布局面板將確保它們仍然正確顯示,並且它們之間的邊距為15px。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.