Delphi TGridPanel - 动态隐藏一些行

时间:2012-12-12 08:31:24

标签: delphi layout tgridpanel

我有像这样的网格面板16 x 4:

enter image description here

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

enter image description here

尽管行大小类型设置为auto:

enter image description here

当没有要显示的内容时,为什么组件没有将行高设置为零?

2 个答案:

答案 0 :(得分:2)

当没有要显示的内容时,为什么组件没有将行高设置为零?

因为只有当该行中的所有列中都没有组件时才会将该行视为空,而不是它们是否可见。所以同样返回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;

第一行显示的用法:

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;

答案 1 :(得分:0)

我有一个hacky解决方案......保持自动调整

Procedure ShowHideControlFromGrid(C:TControl);
begin
  if C.Parent = nil then
    begin
      c.Parent := TWinControl(c.Tag)
    end
  else
    begin
    c.Tag := NativeInt(C.Parent);
    c.Parent := nil;
    end;
end;

procedure TForm4.Button1Click(Sender: TObject);
begin   // e.g. Call
 ShowHideControlFromGrid(Edit5);
 ShowHideControlFromGrid(Edit6);
 ShowHideControlFromGrid(Edit7);
 ShowHideControlFromGrid(Label1);
end;