本文介绍了如何使 StringGrid 的列适合网格的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决方案,但没有任何运气.有谁知道一个简单的方法来做到这一点?例如,我想拉伸网格的第二列以适应网格的宽度!

I've been looking for a long time for a solution without any luck. Does anyone know a simple way to do that? I would like to stretch for example the second colum of my grid to fit the grid's width!

推荐答案

使用 ColWidths 属性,像这样:

Use the ColWidths property, like so:

with StringGrid1 do
  ColWidths[1] := ClientWidth - ColWidths[0] - 2 * GridLineWidth;

对于更健壮和灵活的解决方案,将所有固定列考虑在内并参数化列索引:

And for a more robust and flexible solution, take all fixed columns into account and parameterize the column index:

procedure SetColumnFullWidth(Grid: TStringGrid; ACol: Integer);
var
  I: Integer;
  FixedWidth: Integer;
begin
  with Grid do
    if ACol >= FixedCols then
    begin
      FixedWidth := 0;
      for I := 0 to FixedCols - 1 do
        Inc(FixedWidth, ColWidths[I] + GridLineWidth);
      ColWidths[ACol] := ClientWidth - FixedWidth - GridLineWidth;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetColumnFullWidth(StringGrid1, 4);
end;

这篇关于如何使 StringGrid 的列适合网格的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 06:47