Skip to main content
 首页 » 编程设计

c#之缩放 WinForms TableLayoutPanel

2024年02月24日29softidea

我对 TableLayoutPanel 有疑问,它填充了一个 userControl (DockStyle.Fill)。我遇到了一个麻烦,当这个控件被调整大小时,我需要所有的单元格来改变它们的大小,但只有最后一行和最后一列改变大小(所以整个 tableLayoutPanel 填充控件)。我使用 Bounds 属性更改此 Controller 大小。

假设我写了以下代码:

// creating tableLayoutPanel: 
 
private void createTableLayoutPanel(int count) 
{ 
    tableLayoutPanel = new TableLayoutPanel(); 
    tableLayoutPanel.MouseClick += new MouseEventHandler(this.observe_MouseClick); 
    tableLayoutPanel.AutoSize = true; 
    tableLayoutPanel.ColumnCount = 3; 
    tableLayoutPanel.RowCount = count; 
    tableLayoutPanel.Dock = DockStyle.Fill; 
    tableLayoutPanel.AutoSize = true; 
    this.Controls.Add(tableLayoutPanel); 
} 
 
// resizing: 
 
private void OnMouseWheel(MouseEventArgs e) 
{ 
    this.Bounds = new Rectangle(this.Location.X, this.Location.Y, (int)(this.Width*newScale),(int)(this.Height*newScale)); 
} 

感谢您的帮助。

请您参考如下方法:

对于每一行,将一个项目添加到 TableLayoutPanel 的 RowStyles 集合中,其中 SizeType = SizeType.Percent 和 Height = 100/tableLayoutPanel.RowCount。对 ColumnStyles 集合执行相同的操作。