2013-03-20 82 views
16

我有編程式添加行的TableLayoutPanel。用戶基本上選擇一個屬性,然後顯示在表中以及一些控件。我認爲我在這裏有一個普遍的理解問題,我會盡力解釋它。刪除TableLayoutPanel中的特定行

每一行中的一個控件是一個'刪除'按鈕。該按鈕應該刪除它所在的行。我所做的是將一個事件處理程序添加到按鈕並設置當前的行數。

deleteTalent.Click += (sender, e) => buttonClickHandler(numberOfRows); 

處理程序的代碼:

private void buttonClickHandler(int rowCount) 
{ 
    int count = rowCount - 1; 
    for (int i = count; i < (count + 5); i++) 
    { 
     balanceTable.Controls.RemoveAt(count); 
    } 
    balanceTable.RowStyles.RemoveAt(count); 
    balanceTable.RowCount--; 

} 

我看着它的時間和發揮各地。但我找不到乾淨的解決方案。我也很新的C#

下面是創建一個新的行完整的功能:

private void addBalanceItems(ToolStripMenuItem item) 
{ 
    int numberOfRows = balanceTable.RowCount; 
    if (numberOfRows > 1) 
    { 
     balanceTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.AutoSize)); 

    } 
    balanceTable.Height = numberOfRows * 45; 
    Steigerungsrechner rechner = new Steigerungsrechner(); 
    string tag = item.Tag.ToString(); 

    //change that asap :(
    if (tag == "A") { rechner.column = 1; } 
    if (tag == "B") { rechner.column = 2; } 
    if (tag == "C") { rechner.column = 3; } 
    if (tag == "D") { rechner.column = 4; } 
    if (tag == "E") { rechner.column = 5; } 
    if (tag == "F") { rechner.column = 6; } 
    if (tag == "G") { rechner.column = 7; } 
    if (tag == "H") { rechner.column = 8; } 

    Label talentName = new Label(); 
    talentName.Text = item.Text; 
    talentName.Height = standardHeight; 
    talentName.TextAlign = ContentAlignment.MiddleLeft; 
    talentName.AutoSize = true; 
    Label cost = new Label(); 
    cost.TextChanged += (sender, e) => costChangeHandler(cost); 
    cost.Height = standardHeight; 
    cost.TextAlign = ContentAlignment.MiddleLeft; 
    TextBox startValue = new TextBox(); 
    startValue.TextChanged += (sender, e) => startValueChangeHandler(rechner, startValue, cost); 
    startValue.Height = standardHeight; 
    startValue.TextAlign = HorizontalAlignment.Center; 
    TextBox endValue = new TextBox(); 
    endValue.TextChanged += (sender, e) => endValueChangeHandler(rechner, endValue, cost); 
    endValue.Height = standardHeight; 
    endValue.TextAlign = HorizontalAlignment.Center; 
    Button deleteTalent = new Button(); 
    deleteTalent.Text = "x"; 
    deleteTalent.Click += (sender, e) => buttonClickHandler(numberOfRows); 
    deleteTalent.Height = standardHeight; 

    balanceTable.Controls.Add(talentName); 
    balanceTable.Controls.Add(startValue); 
    balanceTable.Controls.Add(endValue); 
    balanceTable.Controls.Add(cost); 
    balanceTable.Controls.Add(deleteTalent); 
    balanceTable.Visible = true; 
    balanceTable.RowCount++; 
} 

任何幫助將不勝感激! :)

回答

20

是的,從TableLayoutPanel刪除任意行是根本不是直觀。他們真的搞砸了這個設計。

刪除行的唯一方法是設置RowCount屬性。這一點很奇怪,該屬性肯定看起來應該是隻讀的,每次我看到它時,這樣做的代碼看起來都是錯誤的。

但除此之外,這種設計的結果是,你不能從中間刪除行。重置RowCount屬性只會導致行被從底部切掉。

解決方法是一個有點笨拙,多步驟獲得錯誤:

  1. 從要刪除
  2. 如果適用該行刪除控件,移動這些控件到另一行。
  3. 移動其他行中的所有控件,這些控件位於要刪除行的行之​​後。
  4. 最後,通過遞減RowCount屬性的值來刪除最後一行。

快速谷歌搜索顯示有人有written and shared code聲稱這樣做。它在VB.NET中,但應該是easily translated到您的本地方言。

我承認,我已經知道只是踢,並設置RowHeight行我希望「刪除」0。這樣,自動調整爲你做的工作。不過,您可能仍想刪除其中包含的控件。

+2

只是在這裏明確指出,只有當你(至少)**隱藏**它們包含的所有控件時,最後一行纔會被刪除。否則,對「RowCount」的更改不起作用,行仍然可見。 – miroxlav 2014-05-31 18:21:45

9

下面給出了在給定位置移除行的代碼。有一件事要提到:我們通過panel.GetControlFromPosition(...)得到的控件必須是可見的,否則它將返回null而不是不可見的控件。

public void remove_row(TableLayoutPanel panel, int row_index_to_remove) 
    { 
     if (row_index_to_remove >= panel.RowCount) 
     { 
      return; 
     } 

     // delete all controls of row that we want to delete 
     for (int i = 0; i < panel.ColumnCount; i++) 
     { 
      var control = panel.GetControlFromPosition(i, row_index_to_remove); 
      panel.Controls.Remove(control); 
     } 

     // move up row controls that comes after row we want to remove 
     for (int i = row_index_to_remove + 1; i < panel.RowCount; i++) 
     { 
      for (int j = 0; j < panel.ColumnCount; j++) 
      { 
       var control = panel.GetControlFromPosition(j, i); 
       if (control != null) { 
        panel.SetRow(control, i - 1); 
       } 
      } 
     } 

     // remove last row 
     panel.RowStyles.RemoveAt(panel.RowCount - 1); 
     panel.RowCount--; 
    } 
+0

好。另請參見:[如何從tableLayoutPanel控件的特定行和列中移除控件?](https://social.msdn.microsoft.com/Forums/windows/zh-CN/568937e6-6b08-4b22-aeee-5a33159752c3/如何從列表中刪除特定的行和列中的列表面板控件?forum = winforms&prof = required) – Mohsen 2018-01-13 17:50:36

0

刪除現有的rowCount控制在第一

for(int i = 0; i < panel.ColumnCount; i++){ 
    Control Control = panel.GetControlFromPosition(i, rowCount); 
    panel.Controls.Remove(Control); 
    } 

然後取出行

panel.RowStyles.RemoveAt(rowCount-1); 
0

卸下完整表 -

tableLayoutPanel1.Controls.Clear(); 
tableLayoutPanel1.RowStyles.Clear(); 

設置標題再次 -

  tableLayoutPanel.RowCount = 1; 
      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F)); 
      tableLayoutPanel.Controls.Add(new Label() { Text = "MONTH", Font = new Font("Century Gothic", 12, FontStyle.Bold), ForeColor = Color.LightGray }, 0, tableLayoutPanel.RowCount - 1); 
      tableLayoutPanel.Controls.Add(new Label() { Text = "YEAR", Font = new Font("Century Gothic", 12, FontStyle.Bold), ForeColor = Color.LightGray }, 1, tableLayoutPanel.RowCount - 1); 
      tableLayoutPanel.Controls.Add(new Label() { Text = "MEASURED WAFERS", Font = new Font("Century Gothic", 12, FontStyle.Bold), ForeColor = Color.LightGray }, 2, tableLayoutPanel.RowCount - 1); 

3列 - 1行

也許有人可以用我的codesnipped,適當的工作好...

+0

您可以通過在設計視圖中手動設計表格佈局來獲取上述代碼並複製VS生成的代碼。查看錶名後面的'YourForm.Designer.cs'。 – GoTo 2018-02-01 19:49:28

0

你不能完全刪除tablelayoutpanel上的一行,但有一個解決方法:

  1. 刪除該行中的所有控件,如果知道控件的名稱會導致您可以調用dispose方法,則更容易。
  2. 設置使用行式的方法 (例如tablelayoutpanel1.Rowstyle(index).height=2)對我來說這創造了奇蹟的該行可能2px

的高度,連續被完全不管行索引倒塌行。