ok, i need to resize a control based on the layout dimensions. I also need it to resize when the layout dimensions change (e.g. turning device from profile to landscape)
It was my assumption that you just check the dimensions of device and adjust accordingly
e.g.
if ClientHeight > ClientWidth then
fHeader.Height:= ClientHeight Div 6
else
fHeader.Height:= ClientHeight Div 8;
However, where is the best place to put this?
I have tried it in the Resize method before my layout resize and after my layout resize. It does not seem to work!
Well, it works on the initial activation of the form, but when the form is resize, it does not. While in the IDE, i even have to hit the RELOAD button to get it to work.
Below is an example with two components on the form. A TW3HeaderControl aligned to top of form, and a TW3ListBox aligned to client. I would like to adjust the height of the TW3HeaderControl based on whether or not it is in Profile or Landscape
e.g.
unit Form1;
interface
uses
SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms,
SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Layout,
SmartCL.Controls;
type
TForm1 = class(TW3Form)
private
{$I 'Form1:intf'}
fLayout: TLayout;
fHeader: TW3HeaderControl;
fList: TW3ListBox;
protected
procedure InitializeForm; override;
procedure InitializeObject; override;
procedure Resize; override;
end;
implementation
{ TForm1 }
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(fHeader), Layout.Client(fList)]);
end;
procedure TForm1.InitializeObject;
begin
inherited;
{$I 'Form1:impl'}
fHeader:= TW3HeaderControl.Create(self);
fList:= TW3ListBox.Create(self);
end;
procedure TForm1.Resize;
begin
inherited;
if assigned(FLayout) then
begin
fLayout.Resize(self);
if ClientHeight > ClientWidth then
fHeader.Height:= ClientHeight Div 6
else
fHeader.Height:= ClientHeight Div 8;
end;
end;
initialization
Forms.RegisterForm({$I %FILE%}, TForm1);
end.
I even tried overriding the "FormActivated" and placing it there and calling the Resize.
What gives?
UPDATE!!!!!
instead of assigning Layout in the InitializeForm
e.g.
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6),fHeader), Layout.Client(fList)]);
end;
assigning it in the Resize method instead??
e.g.
procedure TForm1.Resize;
begin
inherited;
if ClientWidth > ClientHeight then
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6),fHeader), Layout.Client(fList)])
else
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 8),fHeader), Layout.Client(fList)]);
fLayout.Resize(self);
end;
UPDATE # 2
and, if I add another control onto the form, then there is another issue :(
if I set the size as a static value it works great
procedure TForm1.InitializeObject;
begin
inherited;
{$I 'Form1:impl'}
fHeader:= TW3HeaderControl.Create(self);
fHeader.Height:= 50; //******************************
fHeader.BackButton.Visible:= False;
fHeader.Title.Caption:= 'Menu';
fHeader.Title.AlignText:= taCenter;
fFooter:= TW3HeaderControl.Create(self);
fFooter.Height:= 50; //******************************
fFooter.BackButton.Visible:= False;
fFooter.Title.Caption:= 'Copyright (C) 2016';
fFooter.Title.AlignText:= taCenter;
fList:= TW3ListBox.Create(self);
end;
this works
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(fHeader),
Layout.Bottom(fFooter),
Layout.Client(fList)]);
end;
However, if I set the heights dynamically at runtime based on dimensions, then it does not
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6), fHeader),
Layout.Bottom(Layout.Height(ClientHeight Div 6),fFooter),
Layout.Client(fList)]);
end;