delphi设置TExpander高度 [英] delphi set the TExpander height

查看:112
本文介绍了delphi设置TExpander高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TExpander组件,我在运行时向其中添加了一些TText,我面临的问题是:如何根据TText的数量(如AutoSize)来设置Expander的高度?

i've a TExpander component and i add some TTexts to it at runtime , the issue i'm facing is : how can this Expander's height be set based on the Number of the TTexts , something like AutoSize ?

我正在使用的代码:

procedure TForm1.Button5Click(Sender: TObject);
var
  _DailyEvent:TDailyEvents;
  Eventstext:TText;
  _Y:Integer;
begin
    _Y:=10;
    For _DailyEvent in DailyEventsList do
    begin
             Eventstext:=TText.Create(Self);
             Eventstext.Position.Y := _Y;
             Eventstext.Align:=TAlignLayout.Top;
             Eventstext.Height:=25;
             Eventstext.TagString:=_DailyEvent.EventID;
             Eventstext.Text:=_DailyEvent.EventName;
             Eventstext.Parent:=Expander1;
             inc(_Y, 15);
    end;
    Expander1.Height:=?   

end;

这就是我得到的

谢谢.

推荐答案

实际上,当您将Eventstext.Parent设置为expander1时,此对象已添加到受保护的字段FContent中.因此,如果要将实际大小计算为所有内部控件的总和,则必须获得此字段.

Actually, when you set Eventstext.Parent to expander1, this object added to protected field FContent. So, if you want calculate real size as sum of all inner controls, you must get this field.

您可以像这样覆盖" TExpander类:

You can "override" TExpander class like this:

type
  // declare new TExpander class before form declaration
  // thanks to this declaration we have access to protected fields
  TExpander = class(FMX.StdCtrls.TExpander)
  protected
    procedure DoExpandedChanged; override;
  public
    function GetRealRect: TRectF;
  end;

  TForm2 = class(TForm)
    expndr1: TExpander; // this is our new class, not "standart" TExpander
    btnAdd10: TButton;
    btnDelLast: TButton;
    procedure btnAdd10Click(Sender: TObject);
    procedure btnDelLastClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.btnAdd10Click(Sender: TObject);
var
  Eventstext: TText;
  i: integer;
  _Y: integer;
begin
  _Y := 10;
  For i := 1 to 10 do
  begin
    Eventstext := TText.Create(Self);
    Eventstext.Position.Y := _Y;
    Eventstext.Align := TAlignLayout.Top;
    Eventstext.Height := 25;
    Eventstext.Text := i.ToString;
    Eventstext.Parent := expndr1;
    inc(_Y, 25);
  end;
  // of course, this is not real Autosize,
  // you can override AddObject in TExpander and change size in it,
  // but you can`t get access to RemoveObject in FContent...
  // thus, "AutoSize" will be limited only to adding items.
  // I think the current way is much better than override AddObject...
  expndr1.SetBoundsRect(expndr1.GetRealRect); 
  // or expndr1.height:=expndr1.GetRealRect.Height;
end;

procedure TForm2.btnDelLastClick(Sender: TObject);
begin
  if expndr1.FContent.ChildrenCount <> 0 then
  begin
    expndr1.FContent.Children[expndr1.FContent.ChildrenCount - 1].Release;
    expndr1.SetBoundsRect(expndr1.GetRealRect);
  end;
end;

{ TExpander }

procedure TExpander.DoExpandedChanged;
begin
  inherited;
  SetBoundsRect(GetRealRect);
end;

function TExpander.GetRealRect: TRectF;
var
  i: integer;
  LControl: TControl;
begin
  // above FContent are Button, Text and Checkbox
  Result.TopLeft := AbsoluteRect.TopLeft;
  Result.BottomRight := FContent.AbsoluteRect.TopLeft;

  if FIsExpanded then
    for i := 0 to FContent.ChildrenCount - 1 do
      if FContent.Children[i] is TControl then
      begin
        LControl := TControl(FContent.Children[i]);
        if LControl.Visible then
          UnionRectF(Result, Result, LControl.ChildrenRect);
      end;
  if Result.Width = 0 then // if there are no controls in FContent.
    Result.Width := Width;
end;

这篇关于delphi设置TExpander高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆