【问题标题】:Disable TableLayoutPanel rows autoscaling禁用 TableLayoutPanel 行自动缩放
【发布时间】:2022-01-05 14:53:21
【问题描述】:

我想禁用 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;

【问题讨论】:

    标签: c# winforms autoscroll tablelayoutpanel


    【解决方案1】:

    百分比尺寸类型已损坏。 TableLayoutPanel 的滚动条也有问题,不会收缩。 Here

    我将这些属性用于表格:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      相关资源
      最近更新 更多