【问题标题】:TableLayoutPanel scrollbars bugged at specific amount of rows?TableLayoutPanel 滚动条在特定数量的行处被窃听?
【发布时间】:2017-07-12 23:52:05
【问题描述】:

我使用 TableLayouPanel,以编程方式添加每行高度为 50 像素的行。 TLP y 大小为 217 像素,因此添加 5 行会产生一个垂直滚动条,但也会产生一个水平滚动条

当我添加另一行 - 仍然是垂直滚动条 - 水平滚动条消失

尝试将 TLP 的大小更改为 200 到 250 之间的任何值,但保持不变。

如何消除这种行为,因为任何数量的行但 5 都可以正常工作?

来自设计器的代码并添加行:

System.Windows.Forms.TableLayoutPanel tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.ColumnCount = 3;
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
tableLayoutPanel1.RowCount = 1;
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel1.Size = new System.Drawing.Size(709, 217);


tableLayoutPanel1.RowStyles.RemoveAt(0);
for (int i = tableLayoutPanel1.Controls.Count - 1; i >= 0; i--) {
    tableLayoutPanel1.Controls[i].Dispose();
}
for(int i = 0; i < 5; i++) {
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
    tableLayoutPanel1.Controls.Add(new Control(), 0, i);
    tableLayoutPanel1.Controls.Add(new Control(), 1, i);
    tableLayoutPanel1.Controls.Add(new Control(), 2, i);
}
if (!tableLayoutPanel1.VerticalScroll.Visible){ //false for 5
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
    tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
}

tl;博士为什么会有一个 5 行的水平滚动条以及如何使内容再次适合外部控件所以没有?


想通过添加另一行并再次删除来解决此问题,但由于TableLayoutPanel 没有给出 fck,最后一行自动占用已删除行的“剩余”空间......至少这是我的目前的进展看起来像,不确定我是否应该将所有内容都更改为DataGridView

【问题讨论】:

  • 与主要问题无关——但您是否考虑过使用DataGridView 以“行列”方式显示动态数据
  • @Fabio 很遗憾,没有。但是我再也不想再使用TableLayoutPanel,我已经遇到了很多问题,而且我什至没有处理我项目的最大部分,因为我一直需要修复一些由 TLP 引起的布局问题。感谢您为未来的项目推荐DataGridView,但不知道是否值得再切换(关于我遇到了多少错误:可能,但当然你总是希望“这是最后一个,一切正常如果你解决了这个")
  • @Fabio 尝试用 DataGridView 替换 TableLayoutPanel,但在创建我自己的 DataGridViewColumn 时遇到了一些无法解决的困难。返回尝试使用 TableLayoutPanel
  • 为什么需要创建自己的DataGridViewColumn?你想在那里显示什么样的数据?
  • @Fabio 另一个网格,一个我不需要更改的网格,所以我尝试制作一个TableLayoutPanel-DataGridViewColumn,因为我已经为它编写了代码并且该网格必须显示标签和文本框,否则我将不得不制作一个DataGridView-DataGridViewColumn 和另一列作为标签。即便如此,我也不得不调整我已经用TableLayoutPanel 处理的几十件事。

标签: c# winforms scrollbar tablelayoutpanel


【解决方案1】:

我尝试了很多不同的方法并找到了适合我的解决方案:

tableLayoutPanel1.RowStyles.RemoveAt(0);
for (int i = tableLayoutPanel1.Controls.Count - 1; i >= 0; i--) {
    tableLayoutPanel1.Controls[i].Dispose();
}

成为

int i;  
while ((i = tableLayoutPanel1.Controls.Count) >= 2) {
    tableLayoutPanel1.Controls[--i].Dispose();      
    tableLayoutPanel1.Controls[--i].Dispose();      
    tableLayoutPanel1.Controls[--i].Dispose();  
}   
while (tableLayoutPanel1.RowStyles.Count > 0) {         
    tableLayoutPanel1.RowStyles.RemoveAt(tableLayoutPanel1.RowStyles.Count - 1);
}

和(如果在需要滚动条之前行数少于空间,则可变大小的空行以保持最后一行的大小)

if (!tableLayoutPanel1.VerticalScroll.Visible){ //false for 5
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
    tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
}

成为

tableLayoutPanel1.Update(); //不是确定是否必要

if (!tableLayoutPanel1.VerticalScroll.Visible) {
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
    tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
} else if (tableLayoutPanel1.HorizontalScroll.Visible) {
    if (tableLayoutPanel1.RowStyles.Count != inp.Count) {
        tableLayoutPanel1.GetControlFromPosition(0, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(1, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(2, inp.Count).Dispose();
        tableLayoutPanel1.RowStyles.RemoveAt(inp.Count);
    } else {
        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
        tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
        tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
        tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
        tableLayoutPanel1.GetControlFromPosition(0, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(1, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(2, inp.Count).Dispose();
        tableLayoutPanel1.RowStyles.RemoveAt(inp.Count);
    }
}

对于任何想知道的人:inp.Count 是实际添加的非空行的数量。

也许可以重新格式化和缩短它,因为它看起来多余,但我工作了足够长的时间让它工作,现在应该没问题。

【讨论】:

    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 2015-01-13
    • 2019-04-20
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多