Delphi TGridPanel - 动态隐藏一些行

tru*_*ker 5 delphi layout tgridpanel

我有一个 16 x 4 的网格面板,如下所示:

在此输入图像描述

有时我想隐藏一些行并将底行向上移动。当我将组件可见属性设置为 false 时,布局不会更新:

在此输入图像描述

尽管如此,行大小类型设置为自动:

在此输入图像描述

当没有任何内容可显示时,为什么组件不将行高设置为零?

TLa*_*ama 3

当没有任何内容可显示时,为什么组件不将行高设置为零?

因为只有当该行的所有列中都没有组件时,该行才会被视为空,而不是它们是否可见。所以同样返回该IsRowEmpty方法。要解决此问题,单元格组件需要通知您其可见性更改。生成此通知时,您可以像该IsRowEmpty方法一样检查该行,只不过您将检查控件是否可见,而不是检查它们是否已分配。根据该方法的结果,您可以将 的大小设置Value为 0 以隐藏该行。

借助插入类,检查行或列中的所有控件是否可见的方法,您可以编写类似这样的内容。当特定行或列中的所有现有控件可见时,这些方法返回 True,否则返回 False:

uses
  ExtCtrls, Consts;

type
  TGridPanel = class(ExtCtrls.TGridPanel)
  public
    function IsColContentVisible(ACol: Integer): Boolean;
    function IsRowContentVisible(ARow: Integer): Boolean;
  end;

implementation

function TGridPanel.IsColContentVisible(ACol: Integer): Boolean;
var
  I: Integer;
  Control: TControl;
begin
  Result := False;
  if (ACol > -1) and (ACol < ColumnCollection.Count) then
  begin
    for I := 0 to ColumnCollection.Count -1 do
    begin
      Control := ControlCollection.Controls[I, ACol];
      if Assigned(Control) and not Control.Visible then
        Exit;
    end;
    Result := True;
  end
  else
    raise EGridPanelException.CreateFmt(sInvalidColumnIndex, [ACol]);
end;

function TGridPanel.IsRowContentVisible(ARow: Integer): Boolean;
var
  I: Integer;
  Control: TControl;
begin
  Result := False;
  if (ARow > -1) and (ARow < RowCollection.Count) then
  begin
    for I := 0 to ColumnCollection.Count -1 do
    begin
      Control := ControlCollection.Controls[I, ARow];
      if Assigned(Control) and not Control.Visible then
        Exit;
    end;
    Result := True;
  end
  else
    raise EGridPanelException.CreateFmt(sInvalidRowIndex, [ARow]);
end;
Run Code Online (Sandbox Code Playgroud)

第一行显示的用法:

procedure TForm1.Button1Click(Sender: TObject);
begin
  // after you update visibility of controls in the first row...
  // if any of the controls in the first row is not visible, change the 
  // row's height to 0, what makes it hidden, otherwise set certain height
  if not GridPanel1.IsRowContentVisible(0) then
    GridPanel1.RowCollection[0].Value := 0
  else
    GridPanel1.RowCollection[0].Value := 50;
end;
Run Code Online (Sandbox Code Playgroud)