簡體   English   中英

修復TableLayoutPanel中每行的行高

[英]Fix row height of every row in TableLayoutPanel

我正在使用Windows c#。

首先,根據我的需要,那些不能改變的事情如下:

  1. TableLayoutPanel的大小是固定的。
  2. 列的總數是固定的。

現在,我要為所有行的固定高度,但由於增加了行,如果我的設置RowStyle屬性Percent100.0F然后3到4個項目工作正常,但經過4-5項,在一個控制行覆蓋另一行的控件。

我已經搜索了這個,但我無法得到正確的答案。 我也嘗試過RowStyleAutoSizePercentAbsolute屬性,即使它不起作用。

那該怎么辦?怎么辦? 我怎樣才能做到這一點?

最終,我想像Windows C#的DataGridView一樣。

提前致謝....

我正在使用WinForms ...示例代碼在這里..

int cnt = tableLayout.RowCount = myDataTable.Rows.Count;

tableLayout.Size = new System.Drawing.Size(555, 200);

for (int i = 1; i <= cnt; i++)
{

    Label lblSrNo = new Label();
    lblSrNo.Text = i.ToString(); 

    TextBox txt = new TextBox();
    txt.Text = ""; 
    txt.Size = new System.Drawing.Size(69, 20);

    tableLayout.Controls.Add(lblSrNo, 0, i - 1);
    tableLayout.Controls.Add(txt, 1, i - 1);
}

tableLayout.RowStyles.Clear();

foreach (RowStyle rs in tableLayout.RowStyles)                
    tableLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));

標簽和文本框適用於4-5#行,但每當#of行(在這種情況下,for循環中的變量cnt)增加時,行會相互覆蓋,這是一個控件覆蓋到另一個... I拖放了TableLayoutPanel控件並手動創建了一行和兩列。

所以請告訴我該怎么做。

我自己仍然是tableLayoutPanels的新手,但我注意到在代碼的底部,你正在清除集合中的所有行樣式,然后你試圖在foreach循環中迭代它們。

你這樣做了:

tableLayout.RowStyles.Clear();   //now you have zero rowstyles

foreach (RowStyle rs in tableLayout.RowStyles)   //this will never execute
    tableLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));

試試這個。

TableLayoutRowStyleCollection styles =
    tableLayout.RowStyles;
foreach (RowStyle style in styles){
    // Set the row height to 20 pixels.
    style.SizeType = SizeType.Absolute;
    style.Height = 20;
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

編輯:我剛剛意識到添加N行不會添加可以迭代的N行樣式。 我認為發生的事情是你要添加N行,但沒有一行有行樣式。

我想你可以清除()行樣式,然后添加類似於你已經在做的N行樣式。

有兩種方法可以增加表格布局面板的行高。

  1. 請查看以下鏈接: https//social.msdn.microsoft.com/Forums/windows/en-US/d80db8e1-d6cc-48b8-957f-0f73263c6d4a/how-to-change-the-row-height-of- A-TableLayoutPanel中,在運行時?論壇=的WinForms

    它通過在類后面設置YourTableLayoutPanel.RowStyles [index] .Height int來指定。

  2. 另一種方法是在UI的設計器中設置行高。 通過UI,進入面板的Rows屬性,選擇行並使用百分比或絕對值設置所需的高度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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