从tableLayoutPanel移除文本框的c#问题

本文关键字:问题 文本 tableLayoutPanel | 更新日期: 2023-09-27 18:02:25

你好,我目前正在一个窗口上工作,从那里我需要能够添加和删除一些文本框(和标签)与一个按钮单击。我必须有它设置在一个tableLayoutPanel和一旦"添加"被点击一个标签和5个文本框必须出现在同一行,然后当我点击"删除"他们必须消失,隐藏将不起作用的数据需要从他们在以后的阶段,但这不是一个问题atm。

问题在于删除(我可以很好地添加它们,正如您将在下面看到的),我知道发生了什么,可以猜测为什么,但我需要找到一个替代解决方案>.<</p>

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

}

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

有任何想法如何让这个工作,我将接受不得不改变它几乎完全,但正如我所说,它必须在TableLayoutPanel>.<</p>

欢呼,Jmaru7

从tableLayoutPanel移除文本框的c#问题

这对我来说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;
                }
            }
        }
    }

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