C# – TableLayoutPanel Cutting Off Label String

clabeltablelayoutpanelwinforms

I'm creating a table layout panel to display the values from a dictionary, but the table layout panel keeps cutting the Label controls I put into the cells off at 14 characters. I've tried to fiddle with the ColumnStyles of my table layout panel but none of the options will make the Label control actually 'fit' into the cell. I've tried all the available column style SizeTypes:

Auto-Size (labels with text values are cropped at 14 characters ("1234567890ABCD") every time, though columns with no controls present (spacers) are shrunk to nothing)

Percentage (no effect whatsoever – no columns got wider, even when I weighted the column types (value, key, space) to be different sizes).

Absolute (makes the columns x pixels wide, but the labels are STILL cut off at 14 characters – even if the cell is 1,000 pixels wide)

I've also tried messing with the Size property of the label, but I can't edit that because I "Cannot modify the return value of 'System.Windows.Forms.Control.Size' because it is not a variable" (whatever that means).

So, having exhausted all those options, how do I make the full label appear in the table cell without being cut off at 14 characters?

Here's the code that's generating the table layout panel. It's using a custom class I built (GridDisplay) that keeps a list of objects (GridDisplayCell) that contain a Control, a row number, a column number, and a few other fields. The class lets me add/remove/move/insert controls to the list and then build the table layout all in one go with the Generate() function (rather than determine it's size in advance or re-size it as I add items).

       private void FillInCustomerData()
    {
        GridDisplay grid = new GridDisplay(tl_TopLeft);
        int rowMax = 8;
        int columnLabelIndex = 0;

        int curRow = 0;
        int curCol = 0;

        foreach (var item in DD.AllCustomerData["BasicInfo"]) //Dictionary<string, object>
        {
            if (curRow == rowMax)
            {
                curRow = 0;
                curCol = columnLabelIndex + 2; //1 for key column, 1 for value column
            }

            var keyLabel = new Label();
            keyLabel.Text = item.Key;

            var valueLabel = new Label();
            valueLabel.Text = (item.Value == null || item.Value.ToString() == "") ? "NA" :  "1234567890ABDCDEF"; //item.Value.ToString()

            var key = grid.AddItem(new GridDisplayCell(item.Key, keyLabel), item.Key, curRow, curCol);
            // Function Definition: GridDisplay.AddItem(GridDisplayCell(string cellName, Control control), string cellName, int rowNumber, int colNumber)                
            var value = grid.AddItem(new GridDisplayCell(item.Key + "Value", valueLabel), item.Key + "Value", curRow, curCol+1);

            curRow++;
        }

        grid.WrapMode = false;
        grid.AutoSize = true;

        grid.Generate();

        //experimenting with column sizes. NOT WORKING
        foreach (ColumnStyle cs in grid.Table.ColumnStyles)
        {
            cs.SizeType = SizeType.AutoSize;
        }            
    }

And here's the chunk of my generate function which actually adds the controls to the TableLayoutPanel: (_cells is the list of GridDisplayCells, and AutoSize is a property of GridDisplay in this case (not the TableLayoutPanel's AutoSize property))

 foreach (var cellItem in _cells)
            {
                if (AutoSize == false && ValidateSize(cellItem.Value.Column, cellItem.Value.Row, false) == false)
                {
                    continue; //the cell was outside the range of the control, so we don't add it.
                }

                _table.Controls.Add(cellItem.Value.CellControl, cellItem.Value.Column, cellItem.Value.Row);
            }

Any help is appreciated.

Best Answer

Fixed the problem. I needed to set the Label's AutoSize property to true.

Related Topic