dotnet/winforms

TableLayoutPanel.RowCount doesn't seem to add new rows (similarly for TableLayoutPanel.ColumnCount

Open

#3.578 aberto em 14 de jul. de 2020

Ver no GitHub
 (5 comments) (0 reactions) (0 assignees)C# (922 forks)batch import
:beetle: bug:books: documentationhelp wanted

Métricas do repositório

Stars
 (4.100 stars)
Métricas de merge de PR
 (Mesclagem média 14d 22h) (56 fundiu PRs em 30d)

Description

  • .NET Core Version: 3.1.301

  • Have you experienced this same bug with .NET Framework?: Yes, this was made in .Net Framework 4.7.2 (according to project information).

Problem description: The following is my class. It's straightforward and just contains a table. The table is 6x6 in the Designer (for testing).

Table here is a TableLayoutPanel.

public partial class Board : UserControl
    {
        public int BoardSize { get; private set; }

        public Board(int size)
        {
            InitializeComponent();
            BoardSize = size;
            Table.RowCount = size;
            Table.ColumnCount = size;

            float val = 100 / size;
            for (int i = 0; i < size; i++)
            {
                Table.RowStyles[i].Height = val; // exception raised here
                Table.ColumnStyles[i].Width = val;
                for (int j = 0; j < size; j++)
                {
                    Table.SetCellPosition(new Button(), new TableLayoutPanelCellPosition(i, j));
                }
            }

            // Table.CellPaint += Table_CellPaint;
        }

Error:

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'

It turns out, the error occurs when it tries to reach index 6 (which is basically the RowCount in the Designer).

I'm not sure why this is occurring, as I just set new values for the RowCount and ColumnCount a few lines before.

Expected behavior: Index should not be out of range for Table.RowStyles[i] when it's less than Table.RowCount.

Minimal repro: Not sure what "repro" means...

Guia do colaborador