dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.37k stars 966 forks source link

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

Open osamakawish opened 4 years ago

osamakawish commented 4 years ago

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...

RussKie commented 4 years ago

I think our documentation is to blame for the confusion - the RowCount property doesn't do what you think it does. It does not allocate or create any rows for you, it sets the maximum number of rows the panel will have.

https://github.com/dotnet/winforms/blob/fe6dba96f55d26b56392254e4406d8be2e3a5d52/src/System.Windows.Forms/src/System/Windows/Forms/TableLayoutPanel.cs#L155-L168

https://github.com/dotnet/winforms/blob/fe6dba96f55d26b56392254e4406d8be2e3a5d52/src/System.Windows.Forms/src/System/Windows/Forms/TableLayoutSettings.cs#L124-L132

osamakawish commented 4 years ago

@RussKie

It does not allocate or create any rows for you, it sets the maximum number of rows the panel will have.

There's more to the confusion than that. If the property was renamed MaxRows or MaxRowCount instead of RowCount, it would also be less confusing.

RussKie commented 4 years ago

We can't rename public API, this would be a massively breaking change.

JeremyKuhne commented 1 year ago

If someone wants to clarify documentation here the help would be welcome.

ghost commented 1 year ago

This issue is now marked as "help wanted", and we’re looking for a community volunteer to work on this issue. If we receive no interest in 180 days, we will close the issue. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!