1

您好,我目前正在开发一个窗口,我需要能够通过单击按钮添加和删除多个文本框(和标签)。我必须将其设置在 tableLayoutPanel 中,单击“添加”后,标签和 5 个文本框必须出现在同一行上,然后当我单击“删除”时,它们必须消失,隐藏将无法根据数据需要在稍后阶段从他们那里取走,但这不是问题。

问题在于删除(我可以很好地添加它们,如下所示)我知道发生了什么并且可以猜到原因,但我需要找到一个替代解决方案>.<

 public partial class Form2 : Form
 {
    int Count = 1;
    int rowIndex = 2, colIndex = 1;
    Label Label;
    TextBox Value;
    TextBox Weight;
    TextBox Width;
    TextBox Height;
    TextBox Length;

 private void button1_Click(object sender, EventArgs e)
    {
        if (Count <= 9)
        {
            Count += 1;
            rowIndex += 1;
            tableLayoutPanel10.RowCount = +1;
            AddLot(Count);
            if (Count > 9)
                button1.Enabled = false;
        }

        button2.Enabled = true;

    }

    private void button2_Click(object sender, EventArgs e)
    {


        if (Count == 2)
        {
            tableLayoutPanel10.Controls.Remove(Label);
            tableLayoutPanel10.Controls.Remove(Value);
            tableLayoutPanel10.Controls.Remove(Weight);
            tableLayoutPanel10.Controls.Remove(Width);
            tableLayoutPanel10.Controls.Remove(Height);
            tableLayoutPanel10.Controls.Remove(Length);
            Count -= 1;
            rowIndex -= 1;
            button2.Enabled = false;
        }
        else
        {
            tableLayoutPanel10.Controls.Remove(Label);
            tableLayoutPanel10.Controls.Remove(Value);
            tableLayoutPanel10.Controls.Remove(Weight);
            tableLayoutPanel10.Controls.Remove(Width);
            tableLayoutPanel10.Controls.Remove(Height);
            tableLayoutPanel10.Controls.Remove(Length);
            Count -= 1;
            rowIndex -= 1;
            button1.Enabled = true;
        }


    }

    private void AddLot(int Count)
    {

        Label = new Label();
        Label.Dock = DockStyle.Fill;
        Label.Text = Count.ToString();
        Label.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

        Value = new TextBox();
        Value.Dock = DockStyle.Fill;

        Weight = new TextBox();
        Weight.Dock = DockStyle.Fill;

        Width = new TextBox();
        Width.Dock = DockStyle.Fill;

        Height = new TextBox();
        Height.Dock = DockStyle.Fill;

        Length = new TextBox();
        Length.Dock = DockStyle.Fill;

        tableLayoutPanel10.Controls.Add(Label, colIndex - 1, rowIndex);
        tableLayoutPanel10.Controls.Add(Value, colIndex, rowIndex);
        tableLayoutPanel10.Controls.Add(Weight, colIndex + 1, rowIndex);
        tableLayoutPanel10.Controls.Add(Width, colIndex + 2, rowIndex);
        tableLayoutPanel10.Controls.Add(Height, colIndex + 3, rowIndex);
        tableLayoutPanel10.Controls.Add(Length, colIndex + 4, rowIndex);
    }


}

当我尝试删除时,所发生的只是最后添加的标签/文本框行被删除,然后只有行索引/计数在之后的任何时钟上减少。

任何如何让它工作的想法,我都会接受几乎完全改变它,但正如我所说,它必须在 TableLayoutPanel 中完成 >.<

干杯,Jmaru7

4

2 回答 2

1

这对我来说 100% 有效,我花了一个小时研究它:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tableLayoutPanel1.RowCount = 1;
            tableLayoutPanel1.ColumnCount = 6;
            removeButton.Enabled = false;
        }

         private void addButton_Click(object sender, EventArgs e)
        {
            int index = tableLayoutPanel1.RowCount - 1;
            Label label = new Label();
            TextBox Value = new TextBox();
            TextBox Weight = new TextBox();
            TextBox Width = new TextBox();
            TextBox Height = new TextBox();
            TextBox Length = new TextBox();

            label.Dock = DockStyle.Fill;
            label.Text = (index + 1).ToString();
            label.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

            Value.Dock = DockStyle.Fill;

            Weight.Dock = DockStyle.Fill;

            Width.Dock = DockStyle.Fill;

            Height.Dock = DockStyle.Fill;

            Length.Dock = DockStyle.Fill;

            int i = 0;
            tableLayoutPanel1.Controls.Add(label, i++, index);
            tableLayoutPanel1.Controls.Add(Value, i++, index);
            tableLayoutPanel1.Controls.Add(Weight, i++, index);
            tableLayoutPanel1.Controls.Add(Width, i++, index);
            tableLayoutPanel1.Controls.Add(Height, i++, index);
            tableLayoutPanel1.Controls.Add(Length, i++, index);

            tableLayoutPanel1.RowCount += 1;

            if (tableLayoutPanel1.RowCount > 9)
            {
                addButton.Enabled = false;
            }

            if (tableLayoutPanel1.RowCount > 0)
            {
                removeButton.Enabled = true;
            }
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            if (tableLayoutPanel1.RowCount > 0)
            {
                int startIndex = ((tableLayoutPanel1.RowCount - 1) * 6) - 1;

                for (int i = 0; i < 6; i++)
                {
                    tableLayoutPanel1.Controls.RemoveAt(startIndex--);
                }

                tableLayoutPanel1.RowCount -= 1;

                if (tableLayoutPanel1.RowCount == 0)
                {
                    removeButton.Enabled = false;
                }

                if (tableLayoutPanel1.RowCount <= 9)
                {
                    addButton.Enabled = true;
                }
            }
        }
    }
于 2011-07-19T11:52:49.360 回答
0

您只保留最后添加的一组控件,您应该保留所有这些控件。现在,当您删除最后添加的控件时,下一次删除使用对已删除的引用,因此它不会有任何影响。最好是为一组控件创建一些存储类并将它们保存在某种集合中。

于 2011-07-19T11:38:12.183 回答