I have a TableLayoutPanel which is filled with rows at runtime using a text file (get each row from the text file, and put it in cells contained in new rows). Code Looks like this:
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++;
}
}
Although I want to be able to manually edit the source, so I wrote a method which would delete everything new created, but seem to be stuck here, because it doesn't work:
private void clearPaths()
{
int c = UrlRows.Length - 1;
while (c <= UrlRows.Length - 1)
{
TBP.RowStyles.RemoveAt(c); //Remove from TableLayoutPanel
c--;
}
}
//The Code Stops at: TableLayoutPanel.RowStyles.RemoveAt(c);(while Debugging) //and the error reads : "Object reference not set to an instance of an object" Update: I managed to get out of the Error, my Problem now, after I say RemoveAt, nothing seems to be removed Does anybody know what it is that I could do?
I Googled and Binged a solution to this problem for DAYS and couldn't find one. I finally came with a solution that works!
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();
In my solution, I do not want to remove the first row.