C# 删除 TableLayoutPanel 中的特定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15535214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing a specific Row in TableLayoutPanel
提问by sebsn
I have TableLayoutPanel that I programatically add Rows to. The User basically choses a Property and that is then displayed in the table along with some controls. I think I have a general understanding problem here and I will try to explain it.
我有以编程方式添加行的 TableLayoutPanel。用户基本上选择了一个属性,然后与一些控件一起显示在表格中。我想我在这里有一个一般的理解问题,我会尝试解释它。
One of the Controls in every row is a 'delete'-Button. That button should delete the row it is in. What I did is add an eventhandler to the button and set the current rowcount.
每行中的一个控件是“删除”按钮。该按钮应该删除它所在的行。我所做的是向按钮添加一个事件处理程序并设置当前行数。
deleteTalent.Click += (sender, e) => buttonClickHandler(numberOfRows);
Code of the handler:
处理程序代码:
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--;
}
I looked at it for hours and played around. But I can't find a working clean solution. I'm also pretty new to C#
我看了好几个小时,四处玩耍。但我找不到一个有效的清洁解决方案。我对 C# 也很陌生
Here's the complete Function that creates a new row:
这是创建新行的完整函数:
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++;
}
Any help would be greatly appreciated! :)
任何帮助将不胜感激!:)
采纳答案by Cody Gray
Yeah, removing an arbitrary row from a TableLayoutPanel is not at allintuitive. They really screwed up the design on this one.
是的,从 TableLayoutPanel 中删除任意行一点都不直观。他们真的把这个设计搞砸了。
The only way to remove rows is by setting the RowCount
property. This alone is strange enough; that property sure seems like it should be read-only and code that does this looks wrong to me every time I see it.
删除行的唯一方法是设置RowCount
属性。仅此一点就够奇怪了;该属性似乎应该是只读的,每次我看到它时,执行此操作的代码在我看来都是错误的。
But beyond that, the consequence of this design is that you cannot remove rows from the middle. Resetting the RowCount
property will just cause rows to be lopped off of the bottom.
但除此之外,这种设计的结果是您无法从中间删除行。重置RowCount
属性只会导致行从底部被剪掉。
The workaround is a bit unwieldy, with multiple steps to get wrong:
解决方法有点笨拙,有多个步骤会出错:
- Remove the controls from the row you want to delete
- If applicable, move those controls to to another row.
- Move all of the controls in the other rows that come after the row you wish to delete up a row.
- Finally, remove the last row by decrementing the value of the
RowCount
property.
- 从要删除的行中删除控件
- 如果适用,请将这些控件移至另一行。
- 将要删除的行之后的其他行中的所有控件向上移动一行。
- 最后,通过递减该
RowCount
属性的值来删除最后一行。
A quick Google search reveals that someone has written and shared codepurporting to do this. It's in VB.NET, but that should be easily translatedinto your native dialect.
快速的谷歌搜索显示有人编写并共享了声称要执行此操作的代码。它在 VB.NET 中,但应该很容易翻译成您的母语方言。
I'll admit that I've been known to just punt and set the RowHeight
of the row I wish to "remove" to 0. This way, autosizing does the work for you. You probably still want to remove the controls it contains, though.
我承认我已经知道我只是RowHeight
将我希望“删除”的行的设置为 0。这样,自动调整大小就可以为您完成工作。不过,您可能仍想删除它包含的控件。
回答by ?ilvinas Rud?ionis
Here is a static class that can help you remove any row by it's index:
这是一个静态类,可以帮助您通过索引删除任何行:
using System.Windows.Forms;
public static class TableLayoutHelper
{
public static void RemoveArbitraryRow(TableLayoutPanel panel, int rowIndex)
{
if (rowIndex >= 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, rowIndex);
panel.Controls.Remove(control);
}
// move up row controls that comes after row we want to remove
for (int i = rowIndex + 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);
}
}
}
var removeStyle = panel.RowCount - 1;
if (panel.RowStyles.Count > removeStyle)
panel.RowStyles.RemoveAt(removeStyle);
panel.RowCount--;
}
}
One thing to mention: controls that we get via panel.GetControlFromPosition(...)
must be visible or it will return null
instead of invisible controls.
需要提及的一件事:我们通过的控件panel.GetControlFromPosition(...)
必须是可见的,否则它将返回null
而不是不可见的控件。
回答by Shree Krishna
Remove existing controls of rowCount
at first
删除现有的控制rowCount
在第一
for(int i = 0; i < panel.ColumnCount; i++){
Control Control = panel.GetControlFromPosition(i, rowCount);
panel.Controls.Remove(Control);
}
Then remove row
然后删除行
panel.RowStyles.RemoveAt(rowCount-1);
回答by Ricardo Fercher
Removingcomplete Table -
删除完整的表 -
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.RowStyles.Clear();
Setyour Headlineof the Table again-
再次设置您的表格标题-
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 Columns - 1 Row
3 列 - 1 行
Maybe someone can use my codesnipped, works proper good...
也许有人可以使用我的代码剪断,效果很好......
回答by sailas mwakurudza
You cannot completely delete a row on tablelayoutpanel
but there is a workaround:
您不能完全删除一行,tablelayoutpanel
但有一个解决方法:
- Remove all the controls in the row, easier if you know the names of the controls cause you can call the dispose method.
- Set the height of the row to maybe
2px
using the row style method (e.g.tablelayoutpanel1.Rowstyle(index).height=2
)
- 删除行中的所有控件,如果您知道控件的名称,则会更容易,因为您可以调用 dispose 方法。
- 将行的高度设置为可能
2px
使用行样式方法(例如tablelayoutpanel1.Rowstyle(index).height=2
)
For me this worked wonders the, row was completely collapsed the row regardless of the row index.
对我来说,这创造了奇迹,无论行索引如何,行都完全折叠了行。