TableLayoutPanel: Delete Rows
本文关键字:Rows Delete TableLayoutPanel | 更新日期: 2023-09-27 18:10:56
我有一个TableLayoutPanel,在运行时使用文本文件填充行(从文本文件中获取每一行,并将其放在新行中包含的单元格中)。代码看起来像这样:
public static string UrlList= @"C:'Users'Berisha'Desktop'URLs.txt";
string[] UrlRows = System.IO.File.ReadAllLines(@UrlList);
private void InitPaths()
{
int a = 0;
int c = 1;
while (a < UrlRows.Length-1)
{
//new label
var label = new Label();
label.Dock = DockStyle.Fill;
label.AutoSize = false;
label.Text = UrlRows[a];
label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
label.Size = new System.Drawing.Size(22, 13);
label.BackColor = System.Drawing.Color.Transparent;
TBP.Controls.Add(label, 3, c); //Add to TableLayoutPanel
a++;
c++;
}
}
虽然我希望能够手动编辑源代码,所以我写了一个方法,将删除所有新创建的,但似乎卡在这里,因为它不工作:
private void clearPaths()
{
int c = UrlRows.Length - 1;
while (c <= UrlRows.Length - 1)
{
TBP.RowStyles.RemoveAt(c); //Remove from TableLayoutPanel
c--;
}
}
//代码停在:TableLayoutPanel.RowStyles.RemoveAt(c);(while Debugging)//错误读取:"对象引用未设置为对象的实例"更新:我设法摆脱了错误,我的问题现在,在我说RemoveAt之后,似乎没有任何东西被删除有人知道我能做什么吗?
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.RowCount = 0;
我用google和bing搜索了好几天,没有找到解决这个问题的方法。我终于有了一个有效的解决方案!
tableLayoutPanel.SuspendLayout();
while (tableLayoutPanel.RowCount > 1)
{
int row = tableLayoutPanel.RowCount - 1;
for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
{
Control c = tableLayoutPanel.GetControlFromPosition(i, row);
tableLayoutPanel.Controls.Remove(c);
c.Dispose();
}
tableLayoutPanel.RowStyles.RemoveAt(row);
tableLayoutPanel.RowCount--;
}
tableLayoutPanel.ResumeLayout(false);
tableLayoutPanel.PerformLayout();
在我的解决方案中,我不想删除第一行
好的,看看你的第二个编辑,我删除了我的答案,正在添加这个新的。
我真的怀疑这是否有效。你的while循环永远执行。
int c = UrlRows.Length - 1;
while (c <= UrlRows.Length - 1) //C will decrement forever and always be less than or equal
{
TBP.RowStyles.RemoveAt(c); //Remove from TableLayoutPanel
c--;
}
我不太确定你想在这个方法中做什么,如果你想删除所有东西,你最初的方法就可以了。
int c = 1;
while (c <= UrlRows.Length - 1) //You now loop through all elements in TBP
{
TBP.RowStyles.RemoveAt(c); //Remove from TableLayoutPanel
c++;
}