删除特定行中的TableLayoutPanel [英] Removing a specific Row in TableLayoutPanel

查看:689
本文介绍了删除特定行中的TableLayoutPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的TableLayoutPanel,我编程方式添加行。用户基本上的动产一个属性,并且然后沿与一些控制显示在表中。我想我有一个大致的了解问题就在这里,我将尽力来解释它。

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.

一每一行中的控件是一个delete'-按钮。该按钮应该删除该行就在。我所做的是一个事件处理程序添加到按钮并设置当前的行数。

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

$ C $处理程序的C:

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

}

我看着它的时间和发挥各地。但我不能找到一个工作干净的解决方案。我也pretty的新的C#

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#

下面是创建一个新的行完整的功能:

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++;
}

任何帮助将是很大的AP preciated! :)

Any help would be greatly appreciated! :)

推荐答案

是啊,从一个TableLayoutPanel删除的任意行的并不的直观。他们真的搞砸了的设计在这一个。

Yeah, removing an arbitrary row from a TableLayoutPanel is not at all intuitive. They really screwed up the design on this one.

要删除行的唯一方法是通过设置<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.rowcount.aspx"><$c$c>RowCount属性。仅此一点是自叹不如;这东西肯定看起来像它应该是只读和code,这是否看起来我错了,每次我看到了。

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.

但除此之外,这个设计的结果是,你不能从中间删除行。重置行数属性将只是导致行被砍掉的底部。

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.

解决方法是一个有点笨拙,多步骤获得错误的:

The workaround is a bit unwieldy, with multiple steps to get wrong:

  1. 从要删除的行中删除控制
  2. 如果适用,将这些控件到另一行。
  3. 将所有的控件在要删除一排的行之后来到其他行。
  4. 最后,通过递减行数属性的值删除的最后一行。
  1. Remove the controls from the row you want to delete
  2. If applicable, move those controls to to another row.
  3. Move all of the controls in the other rows that come after the row you wish to delete up a row.
  4. Finally, remove the last row by decrementing the value of the RowCount property.

一个快速谷歌搜索发现,有人<一个href="http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/50d87be6-bbcb-4194-9a3c-b90633a12112">written和共享code 看来是做到这一点。这是在VB.NET,但应该是方便地转换到您的家乡话。

A quick Google search reveals that someone has written and shared code purporting to do this. It's in VB.NET, but that should be easily translated into your native dialect.

我得承认,我已经知道,只是踢,并设置的rowHeight 我要删除为0。通过这种方式,排,自动调整大小呢为你的工作。你可能还是要删除它包含的控件,虽然。

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.

这篇关于删除特定行中的TableLayoutPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆