簡體   English   中英

tablelayoutpanel單元格未在c#win表單中均勻填充

[英]tablelayoutpanel cell not evenly padded in c# win forms

我有一個tablelayoutpanel.And在運行期間我動態生成一些標簽,每個都有一個紫色圓形圖像。現在每個tablelayoutpanel單元格內均勻填充一些圖像/標簽,但有些不是。請看圖像.. 在此輸入圖像描述

頂部2行的標簽均勻填充,而從第3行開始的行縮小。 為什么會這樣? 需要做些什么來擺脫這個問題?提前謝謝。

您應該將ColumnStylesRowStyles設置為合適的值。

例如,對於每個列和行,您可以使用SizeType.PercentSizeType.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM