首页 文章

C# - TableLayoutPanel切断标签字符串

提问于
浏览
1

我正在创建一个表格布局面板来显示字典中的值,但是表格布局面板不断切割我放入单元格的14个字符的Label控件 . 我试图摆弄我的表布局面板的ColumnStyles,但没有一个选项会使Label控件真正“适合”进入单元格 . 我已经尝试了所有可用的列样式SizeTypes:

自动调整大小(带有文本值的标签每次都会裁剪为14个字符(“1234567890ABCD”),尽管没有控件的列(间隔符)会缩小为空)

百分比(没有任何影响 - 即使我将列类型(值,键,空格)加权为不同大小,也没有列更宽 . )

绝对(使列x像素宽,但标签仍然以14个字符切断 - 即使单元格宽度为1,000像素)

我也试过搞乱标签的Size属性,但我无法编辑,因为我“无法修改'System.Windows.Forms.Control.Size'的返回值,因为它不是变量”(无论如何)这意味着) .

因此,在完成所有这些选项后,如何使完整标签出现在表格单元格中而不会被切断为14个字符?

这是生成表格布局面板的代码 . 它使用我构建的自定义类(GridDisplay)来保存包含Control,行号,列号和一些其他字段的对象列表(GridDisplayCell) . 该类允许我添加/删除/移动/插入控件到列表,然后使用Generate()函数一次性构建表格布局(而不是提前确定它的大小或在我添加项目时重新调整大小) .

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;
        }            
    }

这里是我的generate函数的块,实际上将控件添加到TableLayoutPanel :( _ islls是GridDisplayCells的列表,AutoSize是GridDisplay的一个属性,在这种情况下(不是TableLayoutPanel的AutoSize属性))

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);
            }

任何帮助表示赞赏 .

1 回答

  • 10

    解决了这个问题 . 我需要将Label的AutoSize属性设置为true .

相关问题