我有TableLayoutPanel,我以编程方式添加Rows。用户基本上选择了一个属性,然后将其与一些控件一起显示在表中。我想我在这里有一个普遍的理解问题,我会尝试解释它。
每一行中的一个控件是'删除' -Button。该按钮应删除它所在的行。我所做的是向按钮添加一个事件处理程序并设置当前的行数。
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++;
}
任何帮助将不胜感激! :)
答案 0 :(得分:23)
是的,从TableLayoutPanel删除任意行根本不是直观。他们真的搞砸了这个设计。
删除行的唯一方法是设置RowCount
属性。仅这一点就足够奇怪了;这个属性确实看起来应该是只读的,每次看到它时代码都会出错。
但除此之外,这种设计的结果是你无法从中间删除行。重置RowCount
属性只会导致行从底部掉落。
解决方法有点笨拙,有多个步骤出错:
RowCount
属性的值来删除最后一行。Google快速搜索显示有人written and shared code声称要执行此操作。它在VB.NET中,但应该是easily translated到你的母语中。
我承认我已经知道只是向下踢并设置我想要的行的RowHeight
&#34;删除&#34;这样,自动调整功能可以为您完成工作。但是,您可能仍希望删除它包含的控件。
答案 1 :(得分:14)
这是一个静态类,可以帮助您删除任何行的索引:
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--;
}
}
有一点需要提及:我们通过panel.GetControlFromPosition(...)
获得的控件必须是可见的,否则它将返回null
而不是不可见的控件。
答案 2 :(得分:1)
删除完成表格 -
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行
也许有人可以使用我的代码,工作正常......
答案 3 :(得分: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);
答案 4 :(得分:0)
您无法完全删除tablelayoutpanel
上的行,但有一种解决方法:
2px
(例如tablelayoutpanel1.Rowstyle(index).height=2
)对我而言,无论行索引如何,行都完全折叠了行。