【问题标题】:TableLayoutPanel: Delete RowsTableLayoutPanel:删除行
【发布时间】:2013-04-19 11:09:16
【问题描述】:

我有一个 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 之后,似乎没有任何内容被删除 有人知道我能做什么吗?

【问题讨论】:

  • 好的,你的意思是它永远卡在循环中?或者什么“不起作用”?您需要更具描述性。 “它不起作用”可能是您描述问题的最糟糕的方式。此外,值得注意的是,您在 clearPathsInitPaths 中都增加了 a,但仅在 InitPaths 中声明。即使a 是一个全局变量(我希望它不是这样的名称),您似乎也不需要在clearPaths 中增加它。不过,我不明白它是怎么回事,因为您在 InitPaths 中声明的 a 会发生名称冲突
  • 写题的时候不小心用第二种方法忘记了

标签: c# winforms tablelayout tablelayoutpanel


【解决方案1】:
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.RowCount = 0;

【讨论】:

    【解决方案2】:

    我在 Google 上搜索了这个问题的解决方案 DAYS,但找不到。我终于找到了一个可行的解决方案!

    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();
    

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

    【讨论】:

      【解决方案3】:

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

      我真的怀疑这是否有效。您的 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++;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-01
        • 1970-01-01
        • 2016-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多