首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TableLayoutPanel:删除行

TableLayoutPanel:删除行
EN

Stack Overflow用户
提问于 2013-04-25 13:57:10
回答 3查看 13.5K关注 0票数 3

我有一个TableLayoutPanel,它在运行时使用文本文件填充行(从文本文件中获取每一行,并将其放入包含在新行中的单元格中)。代码如下:

代码语言:javascript
复制
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++;
        }
    }

虽然我希望能够手动编辑源代码,但我编写了一个方法,删除所有新创建的内容,但似乎被困在这里,因为它不起作用:

代码语言:javascript
复制
        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之后,似乎没有什么东西被删除

EN

回答 3

Stack Overflow用户

发布于 2013-10-31 21:47:05

几天来,我一直在谷歌上搜索这个问题,却找不到一个解决方案。我终于找到了一个可行的解决方案!

代码语言:javascript
复制
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();

在我的解决方案中,我不想删除第一行。

票数 4
EN

Stack Overflow用户

发布于 2014-11-19 07:10:19

代码语言:javascript
复制
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.RowCount = 0;
票数 4
EN

Stack Overflow用户

发布于 2013-04-25 14:48:24

好的,看看你的第二次编辑,我删除了我的答案,并添加了这个新的答案。

我真的很怀疑这行得通。您的while循环将永远执行。

代码语言:javascript
复制
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--;
}

我不太确定你想用那个方法做什么,如果你想删除所有的东西,那么你最初想要做的事情就会奏效。

代码语言:javascript
复制
int c = 1;
while (c <= UrlRows.Length - 1) //You now loop through all elements in TBP
{
    TBP.RowStyles.RemoveAt(c); //Remove from TableLayoutPanel
    c++;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16216735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档