[英]tablelayoutpanel cell not evenly padded in c# win forms
您應該將ColumnStyles
和RowStyles
設置為合適的值。
例如,對於每個列和行,您可以使用SizeType.Percent
或SizeType.Absolute
定義樣式,並為它們設置相等的值。
在下面的例子中:
TableLayoutPanel
AutoSize
屬性設置為true Form
AutoScroll
屬性設置為true 100/columnCount
)將列樣式設置為SizeType.Percent
SizeType.Absolute
使用等於vaules 30
示例代碼:
int columnCount = 4;
int rowCount = 13;
this.tableLayoutPanel1.ColumnCount = columnCount;
this.tableLayoutPanel1.RowCount = rowCount;
this.tableLayoutPanel1.ColumnStyles.Clear();
this.tableLayoutPanel1.RowStyles.Clear();
this.tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
this.tableLayoutPanel1.BackColor = Color.White;
this.tableLayoutPanel1.AutoSize = true;
for (int i = 0; i < columnCount; i++)
{
this.tableLayoutPanel1.ColumnStyles.Add(
new ColumnStyle(SizeType.Percent, 100 / columnCount));
}
for (int i = 0; i < rowCount; i++)
{
this.tableLayoutPanel1.RowStyles.Add(
new RowStyle(SizeType.Absolute, 30));
}
this.tableLayoutPanel1.SuspendLayout();
for (var i = 1; i <= 50; i++)
{
var label = new Label();
label.Text = i.ToString();
label.Font = new Font(label.Font, FontStyle.Bold);
label.AutoSize = false;
label.Size = new Size(30, 30);
label.Image = Properties.Resources.Circle;
label.ImageAlign = ContentAlignment.MiddleCenter;
label.TextAlign = ContentAlignment.MiddleCenter;
label.Dock = DockStyle.Fill;
this.tableLayoutPanel1.Controls.Add(label);
}
this.tableLayoutPanel1.ResumeLayout();
截圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.