我有一个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);(调试时)//,错误是:“对象引用没有设置为对象的实例”,更新:我成功地摆脱了错误,我现在的问题是,在我说了RemoveAt之后,似乎没有什么东西被删除
发布于 2013-10-31 21:47:05
几天来,我一直在谷歌上搜索这个问题,却找不到一个解决方案。我终于找到了一个可行的解决方案!
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();在我的解决方案中,我不想删除第一行。
发布于 2014-11-19 07:10:19
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.RowCount = 0;发布于 2013-04-25 14:48:24
好的,看看你的第二次编辑,我删除了我的答案,并添加了这个新的答案。
我真的很怀疑这行得通。您的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++;
}https://stackoverflow.com/questions/16216735
复制相似问题