Ali Tor
Ali Tor

Reputation: 2965

TableLayoutPanel doesn't change the last row index after deleting a row

I have added 4 control as myControli-i is the row index- into a TableLayoutPanel. Then I deleted the 3rd row programmatically. But when I call TableLayoutPanel.GetRow(myControl3), it returns 3 as row number instead of 2. Why it doesn't rearrange the row indexes after deleting a row?

enter image description here

private void deleteButton3_Click(object sender, EventArgs e)
    {
        tableLayoutPanel1.Controls.Remove(button3);
        tableLayoutPanel1.RowCount -= 1;
        Debug.WriteLine(tableLayoutPanel1.GetRow(button4));
        //Returns 3
    }

UPDATE: I have updated the code sample to be more clear and uploaded an image. The table layout has 4 AutoSize rows, 1 column. The buttons was added in design time.

Upvotes: 1

Views: 379

Answers (1)

Manta
Manta

Reputation: 517

It appears that you cannot remove a row "in the middle". RowCount remove the last row. What you have to do is :

-remove the control

-move up all the controls after the row you want to delete

-delete row

In your case :

    private void deleteButton3_Click(object sender, EventArgs e)
    {
        tableLayoutPanel1.Controls.Remove(button3);
        tableLayoutPanel1.SetRow(button4, 2);
        tableLayoutPanel1.RowCount -= 1;
        Debug.WriteLine(tableLayoutPanel1.GetRow(button4));
    }

And you can write a simple method to do that...

Upvotes: 2

Related Questions