1

Hello I'm currently working on a windows from where I need to be able to add and remove a number of textboxes (and a lable) with a button click. I have to have it set out within a tableLayoutPanel and Once "Add" is clicked a Label and 5 Text boxes must appear on the same row, and then when I click "Remove" they must dissapear, Hiding won't work as Data needs to be taken from them at a later stage but thats not an issue atm.

The problem is with the removal (I can add them fine as you'll see below) I know whats happening and can guess as to why but I need to find an alternate solution >.<

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


}

All That Happens when I try to remove is the Last added Row of label/textboxes is removed, and then only the rowindex/count decrease on any clocks afterwards.

Any Ideas how to get this to work, I'll accept having to change it almost completely but as I said It must be done in the TableLayoutPanel >.<

Cheers, Jmaru7

4

2 に答える 2

1

これは私にとって100%機能します。私はそれに1時間費やしました:

 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 に答える