TableLayoutPanel cell not resizing to fit its dynamically created contents

81 views Asked by At

I am trying to create a Windows Form (C#) that takes in any number of filters, and then creates a Search window by using those filters to create label and textbox pairs for the user to filter their search by. The results are displayed in a DataGridView.

Here is what the UI should look like:

enter image description here

My current issue is that when I add additional filters, the top section that contains the label/textbox pairs are not resizing to fit all the controls. Instead, it seems that it only properly fits 3 label/textbox pairs, and the rest are getting cut off. I already have that row set to AutoSize so why is it that the cell is not autosizing to fit the contents?

Here is my code:

// initializes outer table layout panel to hold internal table layouts
            TableLayoutPanel outer = new TableLayoutPanel();

            // sets outer table layout properties
            outer.Name = "OuterPanel";
            outer.AutoSize = true;
            outer.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            outer.Padding = new Padding(10, 20, 10, 20);

            // creates rows for each of the dialog sections
            outer.RowStyles.Add(new RowStyle(SizeType.AutoSize)); // contains filter UI (labels, textboxes) and allows it to resize with additional controls
            outer.RowStyles.Add(new RowStyle(SizeType.Percent, 75)); // contains results and allows it to resize with form resizing
            outer.RowStyles.Add(new RowStyle(SizeType.Absolute, 60)); // contains buttons

            // initializes table layout panel to format labels and textboxes
            TableLayoutPanel filterUI = new TableLayoutPanel();

            // sets filter UI properties
            filterUI.Name = "LayoutPanel";
            filterUI.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            filterUI.Padding = new Padding(0, 0, 0, 0);

            // creates columns for labels and textboxes
            filterUI.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            filterUI.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

            // dictionary to keep track of label and textbox pairs
            Dictionary<Label, TextBox> UIPairs = new Dictionary<Label, TextBox>();

            // adds labels and textboxes to the UIPairs dictionary and the filterUI table
            for (int i = 0; i < filters.Count; i++)
            {
                // adds a new row for each pair
                filterUI.RowStyles.Add(new RowStyle(SizeType.AutoSize));

                // creates new pair of label and textbox
                UIPairs.Add(new Label(), new TextBox());

                // sets control names
                UIPairs.Keys.ElementAt(i).Name = filters[i].labelName;
                UIPairs.Values.ElementAt(i).Name = filters[i].textName;

                // sets label attributes
                UIPairs.Keys.ElementAt(i).Text = filters[i].labelText;
                UIPairs.Keys.ElementAt(i).AutoSize = true;
                UIPairs.Keys.ElementAt(i).Padding = new Padding(0, 5, 0, 10);

                // sets textbox attributes
                UIPairs.Values.ElementAt(i).Size = new System.Drawing.Size(200, 20);

                // adds to form
                filterUI.Controls.Add(UIPairs.Keys.ElementAt(i), 0, i);
                filterUI.Controls.Add(UIPairs.Values.ElementAt(i), 1, i);
            }

            // initializes DataGridView table to store results
            DataGridView results = new DataGridView();

            // sets DataGridView properties
            results.Name = "dgvResults";
            results.Height = 300; // default height, but can be resized with resizing of form

            // code ommitted but it just sets the binding source for the DataGridView 

            // initalizes table layout panel to hold buttons
            TableLayoutPanel buttons = new TableLayoutPanel();

            // sets button table properties
            buttons.AutoSize = false;
            buttons.Padding = new Padding(0, 15, 0, 0);

            // creates rows and columns to format buttons
            buttons.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            buttons.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            buttons.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

            // initializes buttons
            Button btnSearch = new Button(); // searches
            Button btnClear = new Button(); // clears textboxes and results
            #endregion

            // sets btnSearch properties
            btnSearch.Name = "btnSearch";
            btnSearch.Text = "Search";
            btnSearch.Height = 35;
            btnSearch.Width = 100;
            btnSearch.AutoSize = false;

            // sets btnClear properties
            btnClear.Name = "btnClear";
            btnClear.Text = "Clear";
            btnClear.Height = 35;
            btnClear.Width = 75;
            btnClear.AutoSize = false;
            btnClear.Click += new EventHandler(btnClear_Click);

            // sets form properties
            this.AutoSize = true; // allows the form to resize according to its internal controls
            this.AcceptButton = btnSearch; // sets default button to search button

            // sets default form width to fit full DataGridView so no column headers are cut off initially
            int contentWidth = 0;
            foreach (DataGridViewColumn col in results.Columns)
            {
                contentWidth += col.Width;
            }
            this.Width = contentWidth + 70;

            // sets docking properties of each UI section
            outer.Dock = DockStyle.Fill;
            filterUI.Dock = DockStyle.Fill;
            buttons.Dock = DockStyle.Right;

            // adds controls to form
            Controls.Add(outer); // adds outer layout to form
            outer.Controls.Add(filterUI, 0, 0); // adds filter UI to outer layout
            outer.Controls.Add(results, 0, 1); // adds DataGridView to outer layout
            outer.Controls.Add(buttons, 0, 2); // adds buttons layout to outer layout
            buttons.Controls.Add(btnClear, 0, 0); // adds clear button to buttons layout
            buttons.Controls.Add(btnSearch, 1, 0); // adds search button to buttons layout
        }
0

There are 0 answers