Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TableLayoutPanel滚动条在特定数量的行中出现错误?_C#_Winforms_Scrollbar_Tablelayoutpanel - Fatal编程技术网

C# TableLayoutPanel滚动条在特定数量的行中出现错误?

C# TableLayoutPanel滚动条在特定数量的行中出现错误?,c#,winforms,scrollbar,tablelayoutpanel,C#,Winforms,Scrollbar,Tablelayoutpanel,我使用一个TableLayouPanel,以编程方式添加每行50像素高的行。TLP y-size为217 px,因此添加5行会产生一个垂直滚动条,但也会产生一个水平滚动条 当我添加另一行时,仍然是垂直滚动条,水平滚动条消失 尝试将TLP的大小更改为200到250之间的任何值,保持不变 如果行数不超过5行,如何消除这种行为 设计器中的代码并添加行: System.Windows.Forms.TableLayoutPanel tableLayoutPanel1 = new System.Window

我使用一个TableLayouPanel,以编程方式添加每行50像素高的行。TLP y-size为217 px,因此添加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);
}
System.Windows.Forms.TableLayoutPanel tableLayoutPanel1=新系统.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(新的System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent,100F));
tableLayoutPanel1.ColumnStyles.Add(新的System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute,50F));
tableLayoutPanel1.ColumnStyles.Add(新的System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute,50F));
tableLayoutPanel1.RowCount=1;
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel1.Size=新系统图尺寸(709217);
tableLayoutPanel1.RowStyles.RemoveAt(0);
对于(int i=tableLayoutPanel1.Controls.Count-1;i>=0;i--){
tableLayoutPanel1.Controls[i].Dispose();
}
对于(int i=0;i<5;i++){
tableLayoutPanel1.RowStyles.Add(新的RowStyle(SizeType.Absolute,50));
tableLayoutPanel1.Controls.Add(新控件(),0,i);
tableLayoutPanel1.Controls.Add(新控件(),1,i);
tableLayoutPanel1.Controls.Add(新控件(),2,i);
}
如果(!tableLayoutPanel1.VerticalScroll.Visible){//false表示5
tableLayoutPanel1.RowStyles.Add(新的RowStyle(SizeType.AutoSize,0));
tableLayoutPanel1.Controls.Add(新控件(),0,inp.Count);
tableLayoutPanel1.Controls.Add(新控件(),1,inp.Count);
tableLayoutPanel1.Controls.Add(新控件(),2,inp.Count);
}
tl;dr为什么有一个5行的水平滚动条,以及如何使内容再次适合外部控件,从而没有滚动条?



考虑通过添加另一行并再次删除来修复此问题,但由于
TableLayoutPanel
不提供fck,最后一行会自动占用已删除行的“剩余”空间…至少我当前的进度是这样的,我不确定是否应该将所有内容都更改为
DataGridView

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

变成

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);
}
和(具有可变大小的空行,如果需要滚动条之前的行数少于空格,则保留最后一行的大小)

变成

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);
}
tableLayoutPanel1.Update()//还不确定是否有必要

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


< P> >可能会重新格式化并缩短它,因为它看起来很冗长,但是我工作得够长了,现在可以工作了。现在应该是好的。

与主要问题无关。但是,您是否考虑使用<代码> DATAGIDVIEW < /代码>来显示“行列”中的动态数据?way@Fabio悲哀地没有。但是我再也不想使用
TableLayoutPanel
,我已经有很多问题了,我甚至没有在我的项目的最大部分上工作,因为我一直都要修复一些由TLP引起的布局问题。感谢您为未来的项目推荐
DataGridView
,但我不知道是否值得再切换(关于我遇到了多少bug:可能吧,但当然您总是希望“这是最后一个,如果您解决了这个问题,一切都会很好”)@Fabio尝试用DataGridView替换TableLayoutPanel,但在创建自己的DataGridViewColumn时遇到了一些无法解决的困难。返回到尝试使用TableLayoutPanel为什么需要创建自己的
DataGridViewColumn
?你想在那里显示什么样的数据?@Fabio另一个网格,一个我不需要更改的网格,所以我尝试制作一个
TableLayoutPanel
-
DataGridViewColumn
,因为我已经为它编写了代码,网格必须显示标签和文本框,否则,我将不得不使用另一列作为标签制作一个
DataGridView
-
DataGridView列
。即使这样,我也不得不调整我已经用
TableLayoutPanel
处理过的几十件事情。
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);
}
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);
    }
}