我想禁用 TableLayoutPanel 的行自動縮放,以便它適合,例如,寬度為 4 列,高度為 3 行,并且自動滾動也可以作業。我應該改變什么?
代碼:
public UserControl()
{
InitializeComponent();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
foreach (Picture picture in Program.gallery)
addImage(picture);
for (int i=0;i<tableLayoutPanel1.ColumnCount;i )
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f/4));
for (int i = 0; i < 99999; i )
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100f/3));
}
TableLayoutPanel 具有:
AutoScroll=true;
AutoSize=false;
ColumnCount=4;
RowCount=3;
Dock=true;
GrowStyle=AddRows;
uj5u.com熱心網友回復:
百分比尺寸型別已損壞。TableLayoutPanel 的滾動條也有問題,并且沒有縮小。這里
我將這些屬性用于表:
AutoScroll=false;
AutoSize=true;
ColumnCount=4;
RowCount=0;
Dock=Top;
GrowStyle=AddRows;
和外部控制屬性:
AutoScroll=true;
檢查行數和禁用未使用的代碼:
private int cellsCount=0;
private const int rows = 3;
private int CellsCount {
get => cellsCount;
set {
cellsCount = value;
int expectedRows = (cellsCount - 1 tableLayoutPanel1.ColumnCount) / tableLayoutPanel1.ColumnCount;
while (expectedRows > tableLayoutPanel1.RowCount)
{
tableLayoutPanel1.RowCount ;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, Height/rows));
}
while (expectedRows < tableLayoutPanel1.RowCount) {
tableLayoutPanel1.RowCount--;
tableLayoutPanel1.RowStyles.RemoveAt(0);
}
}
}
并調整偵聽器的大小以設定行的真實尺寸:
private void tableLayoutPanel1_Resize(object sender, EventArgs e)
{
foreach (RowStyle row in tableLayoutPanel1.RowStyles)
row.Height = Height / rows;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/372223.html
