我创建了一个表格布局面板来显示字典中的值,但是表格布局面板不断切割我放入14个字符的单元格中的Label控件。我试图摆弄我的表格布局面板的ColumnStyles,但没有任何选项会使Label控件实际适合单元格。我已经尝试了所有可用的列样式SizeTypes:C# - TableLayoutPanel切割标签字符串
自动尺寸(带有文本值的标签每次裁剪为14个字符(“1234567890ABCD”),尽管不存在任何控件的列(间隔符)收缩为空)
百分比(没有任何影响 - 即使我将列类型(值,键,空间)加权为不同大小,也没有列变宽。
绝对(使列×像素宽,但唱片公司在14个字符STILL切断 - 即使电池是1000个像素宽)
我也试着与标签的大小属性搞乱,但我不能编辑,因为我“无法修改”System.Windows.Forms.Control.Size“的返回值,因为它不是一个变量”(无论这意味着什么)。
因此,我已经耗尽了所有这些选项,如何在表格单元格中显示完整标签而不会在14个字符处截断?
下面是生成表格布局面板的代码。它使用我构建的自定义类(GridDisplay),它保存包含控件,行号,列号和其他一些字段的对象列表(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;
}
}
这是我的生成功能块,实际上增加了控制到TableLayoutPanel中:(_cells是GridDisplayCells的名单,并自动调整大小是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);
}
任何帮助表示赞赏。