我在.NET 2.0中編寫應用程序,我需要WinForms中的FlowLayoutPanel
的功能。這是我想出了,我現在使用(主要是與標籤)的代碼:CFL中的FlowLayoutPanel
/// <summary>
/// Flowable layout panel
/// </summary>
public partial class FlowLayoutPanel : Panel
{
public FlowLayoutPanel()
{
InitializeComponent();
}
/// <summary>
/// Flow the layout of the panel. Required before presentation.
/// </summary>
public void Layout()
{
int top = 0;
// Set control position
foreach (Control ctrl in this.Controls)
{
ctrl.Top = top;
// Account for the scrollbar
ctrl.Width = this.Width - 17;
top += ctrl.Height;
}
}
}
該面板添加到窗體(在我的情況,動態生成的標籤頁),然後我添加在代碼視圖中的控件,如下所示:
panel.Controls.Add(new Label() { Text = "Item name", Font = boldTahoma });
panel.Controls.Add(new Label() { Text = item.ItemName });
panel.Controls.Add(new Label() { Text = "Category", Font = boldTahoma });
panel.Controls.Add(new Label() { Text = item.Category });
panel.Controls.Add(new Label() { Text = "Quantity", Font = boldTahoma });
panel.Controls.Add(new Label() { Text = item.Quantity });
panel.Layout();
所以我想我有兩個問題。它的工作原理,但有沒有更好的方法來做到這一點(尤其是我不必每次都打電話Layout()
),有沒有辦法讓標籤自動調高?謝謝。