【发布时间】:2014-09-30 09:31:43
【问题描述】:
我有一个可以调整大小的网格。我现在正在努力填充网格中列周围的空白区域。我正在尝试在 FormResize 上实现这一目标。
首先我计算列的总宽度,然后将其与字符串网格宽度进行比较。如果 stringgrid 宽度更大,那么我添加到每列宽度相等部分的空白空间。这是它在 formResize 过程中的样子:
procedure TBDDTool.FormResize(Sender: TObject);
var
totColWidth,i : integer;
begin
totColWidth := 0;
for i := 0 to sgFilePreview.ColCount - 1 do
totColWidth := totColWidth + sgFilePreview.ColWidths[i];
if sgFilePreview.Width > TotColWidth then
begin
for i := 0 to sgFilePreview.ColCount - 1 do
begin
sgFilePreview.ColWidths[i] := round(sgFilePreview.ColWidths[i] +
((sgFilePreview.Width - totColWidth)/(sgFilePreview.colCount)));
end;
end;
end;
这实际上不起作用,因为 sgFilePReview.Width 是我的网格的宽度。而且我不知道如何获得网格内整个空间的宽度,就像每列+剩余的空白空间一样。我怎样才能得到网格的实际宽度?原因 sgFilePreview.Width 返回网格的宽度,但从网格外部看到。
谢谢!
编辑
添加新列
for val in sLineSplitted do
begin
if Pos('#',val) <> 0 then propVal := copy(val,0,pos('#',val)-1)
else propVal := val;
col := col +1;
if (row = 1) then
begin
if (col >1) then
//Add column
sgFilePreview.ColCount := col;
sgFilePreview.Cols[col-1].Text := propVal;
SetLength(aSourceData[row-1],col);
aSourceData[row-1,col-1] := val;
end
else
begin
sgFilePreview.RowCount := row;
SetLength(aSourceData[row-1],col);
aSourceData[row-1, col-1] := val;
sgFilePreview.Cells[col-1, row-1] := propVal;
pnlFileManager.Visible := true;
end;
end;
如果世界大于单元格的宽度,则自动调整列大小以适应单词
procedure TBDDTool.AutoSizeGrid(Grid: TStringGrid);
const
ColWidthMin = 10;
var
C,R,W, ColWidthMax: integer;
begin
for c := 0 to Grid.ColCount - 1 do
begin
ColWidthMax := ColWidthMin;
for R := 0 to Grid.RowCount - 1 do
begin
W := Grid.Canvas.TextWidth(Grid.Cells[C,R]);
if W > ColWidthMax then
ColWidthMax :=W;
end;
Grid.ColWidths[C] := ColWidthMax +5;
end;
end;
【问题讨论】:
-
你不使用
ClientWidth吗? -
@DavidHeffernan 我使用了 clientWidth 但它返回的值与 gird.Width 将返回的值相同
-
那我不明白你的问题
-
@DavidHeffernan 我的网格末尾有一个空白区域。在所有列之后。为了摆脱那个空间,我想把这个空间平等地添加到所有的列上,这样就没有更多的空间了。我不想看到那个空间,这就是我尝试这样做的原因
-
我想你也需要考虑边界线和列之间的线。我将从总宽度开始。然后从该总数中删除所有边框/分隔线,从而使宽度可用。然后遍历将宽度设置为 WidthAvailable div ColCountRemaining 的每一列。在执行每一列时,将 WidthAvailable 递减您刚刚使用的宽度,并将 ColCountRemaining 递减 1。
标签: delphi resize tstringgrid