Delphi 7 edit component creation - delphi

I have a problem according to run-time creation of edit components in Delphi 7.
So when I create TEdit components after the program ran for "some" time it perfectly works.
However, when I create TEdit elements at the Forms OnCreate event, they have a wrong height.
Furthermore the (almost) simultaneously created Shapes have the right height.
Edit:
procedure TTPLVisorForm.CreateZeichen(ZShape : TShape; ZEdit : TEdit; VLeft : integer);
begin
with ZShape do
begin
Width := 50;
Height := 50;
Left := VLeft;
Top := 25;
Shape := stRectangle;
Parent := self.Band;
SendToBack();
end;
with ZEdit do
begin
Text := '#';
Left := VLeft+1;
Top := 26;
Parent := self.Band;
Font.Height := 48;
Width := 48;
Height := 48;
SendToBack;
end;
end;
Getting called by:
procedure TZeichen.Anzeigen(Form : TObject; Left : integer);
begin
self.Form := Form;
self.ZShape := TShape.Create(TTPLVisorForm(self.Form).Band);
self.ZEdit := TEdit.Create(TTPLVisorForm(self.Form).Band);
TTPLVisorForm(Form).CreateZeichen(self.ZShape, self.ZEdit, Left);
end;
Getting called by:
procedure TMagnetband.ErweitereRechts;
var
Zeichen : TZeichenKette;
begin
Zeichen := TZeichenKette.Create;
self.LetztesZeichen.Naechstes := TZeichenKette(Zeichen);
Zeichen.Vorheriges := self.LetztesZeichen;
Zeichen.Zeichen.Anzeigen(self.Form,
self.LetztesZeichen.Zeichen.ZShape.Left +
self.LetztesZeichen.Zeichen.ZShape.Width +
self.Padding);
self.LetztesZeichen := Zeichen;
self.Laenge := self.Laenge+1;
end;
Getting again called by:
procedure TTuringmaschine.ZeichenAnfuegen;
begin
self.Magnetband.ErweitereRechts;
end;
Getting called by:
procedure TTuringmaschine.PanelResize(Sender: TObject);
begin
while self.Magnetband.GetRechtsMax < self.Panel.Width do
self.ZeichenAnfuegen;
end;
Finally gets called by:
Constructor TTuringmaschine.Create(Form : TObject);
var
Breite : integer;
begin
self.Zustand := 0;
self.Form := TTPLVisorForm(Form);
self.Panel := TTPLVisorForm(self.Form).Band;
self.Magnetband := TMagnetband.Create(self.Form);
TTPLVisorForm(Form).Band.OnResize := self.PanelResize;
self.PanelResize(Nil);
//self.CreateMagnetkopf;
end;
And the Constructor is either called at the OnCreate event or on another event.

There's a margin around the text in TEdit control, so if you set the Font.Height to 48, the height of the control won't be exactly 48 if the control has the AutoSize property set to True. I would personally decrease height of the font, and for being sure turn the AutoSize off. Your CreateZeichen method would then look like this:
procedure TTPLVisorForm.CreateZeichen(ZShape: TShape; ZEdit: TEdit;
VLeft: Integer);
begin
with ZShape do
begin
Width := 50;
Height := 50;
Left := VLeft;
Top := 25;
Shape := stRectangle;
Parent := Self.Band;
SendToBack;
end;
with ZEdit do
begin
AutoSize := False;
Text := '#';
Left := VLeft + 1;
Top := 26;
Parent := Self.Band;
Font.Height := 40;
Width := 48;
Height := 48;
SendToBack;
end;
end;

Related

Creating custom button in Delphi with different actions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am creating a custom component, a button-switch like this :
In my "form activate" function, I wrote a for loop in which I call 3 times the button function with a different position parameter like : SwitchButton(30); where 30 is the top position.
What I want to do is assign at these 3 buttons different actions, here some code.
Code for button creation :
procedure TFMain.SwitchButton(posPulsante: Integer);
var
i: Integer;
posDescrizionePulsante: Integer;
strDescrizione: String;
begin
posDescrizionePulsante := 32;
lastPressed := 1;
Pulsante := TPanel.Create(FMain);
BordoPulsante := TShape.Create(self);
LevaPulsante := TPanel.Create(self);
DescrizionePulsante := TLabel.Create(self);
//Proprietà Descrizione
with DescrizionePulsante do
begin
Parent := PComandi;
Top := posDescrizionePulsante;
Left := 100;
Caption := 'Visualizza finestra utenti';
Font.Name := 'Tahoma';
Font.Size := 12;
Font.Style := [fsBold];
Font.Color := clWhite;
end;
//Proprietà Pulsante
with Pulsante do
begin
Parent := PComandi;
ParentColor := false;
ParentBackground := false;
BevelOuter := bvNone;
Color := clWhite;
Width := 57;
Height := 25;
Top := posPulsante;
Left := 20;
Visible := true;
end;
//Proprietà Bordo
with BordoPulsante do
begin
Parent := Pulsante;
Align := alClient;
Brush.Style := bsClear;
Brush.Color := RGB(122,136,201);
Pen.Color := clWhite;
Pen.Style := psSolid;
Pen.Width := 3;
end;
//Proprietà Leva
with LevaPulsante do
begin
Parent := Pulsante;
ParentBackground := false;
ParentColor := false;
BevelOuter := bvNone;
Color := clWhite;
Cursor := crHandPoint;
Width := 23;
Height := 13;
Top := 6;
Left := 28;
LevaPulsante.OnClick := SwitchState;
end;
end;
Code for creating Button object :
procedure TFMain.FormActivate(Sender: TObject);
var
i: Integer;
posPulsante: Integer;
begin
posPulsante := 30;
for i := 1 to 3 do
begin
SwitchButton(posPulsante);
posPulsante := posPulsante + 50;
end;
end;
Would be nice to have some : if SwitchButton 1 is clicked then do something. if SwitchButton 2 is clicked then do something else.
I think this is an acceptable use of the Tag property. You can specify some number, for example the index of the for loop, in each of the panels:
procedure TFMain.FormActivate(Sender: TObject);
var
i: Integer;
posPulsante: Integer;
begin
posPulsante := 30;
for i := 1 to 3 do
begin
SwitchButton(posPulsante, i {For example, add the tag as extra parameter});
posPulsante := posPulsante + 50;
end;
end;
And then set that tag in the button, here using the added parameter indexPulsante.
procedure TFMain.SwitchButton(posPulsante: Integer; indexPulsante: Integer);
var
i: Integer;
posDescrizionePulsante: Integer;
strDescrizione: String;
begin
...
LevaPulsante.Tag := indexPulsante;
end;
And then, in the event handler (which I think you called SwitchState), you'll have the Sender, which is the control that was clicked (the panel, in your case).
procedure TFMain.SwitchState(Sender: TObject);
begin
case (Sender as TComponent).Tag of
1: ShowMessage('You clicked the first. Do something.')
2: ShowMessage('Do something else.')
else
ShowMessage('You clicked another button than 1 or 2');
end;
end;
NB: Tag is introduced in TComponent and therefore also available in TPanel. In the code above I only typecast to TComponent, because it doesn't matter that it's a panel to get the tag, but if you want to use other properties, a more specific cast may be needed. I like to keep the cast generic, to make it easier to make changes like switching to another type than TPanel, for instance when you actually gonna make a component out of this (inherited from TCustomControl?), or use a third party component.

No scrollbars appear in an autoscrollable form

------------------------- ORIGINAL QUESTION -------------------------
Greetings to all Delphi developers! In a Delphi 2006 non MDI application, I create a non-sizeable, autoscrollable, autosizeable form. This is an excerpt from the form's unit:
uses Grid;
TGridFrm = class(TForm)
public
Grid : TGrid;
constructor Create(AOwner : TComponent; Asize : TPoint);
end;
implementation
constructor TGridFrm.Create(AOwner: TComponent; Asize : TPoint);
begin
inherited Create(aowner);
borderstyle := bsSingle; // users are not allowed to resize the form
windowstate := wsNormal;
borderwidth := 0;
autosize := True;
autoscroll := True;
constraints.maxwidth := screen.width - 1;
constraints.maxheight := screen.height - 1;
grid := TGrid.Create(asize.x, asize.y, self);
end;
Now, TGrid is a custom control with its own canvas of course. This is an excerpt from its unit:
TGrid = class (TCustomControl)
public
NoOfCellsX,
NoOfCellsY,
CellSize : integer;
procedure SetZoom(z : integer);
constructor Create(AWidth, AHeight : Integer; AParent : TForm = nil);
end;
implementation
constructor TGrid.Create(AWidth, AHeight : Integer; AParent : TForm = nil);
begin
inherited Create(AParent);
Parent := AParent;
align := alCustom;
left := 0;
top := 0;
end;
procedure TGrid.SetZoom(zoom : integer);
begin
cellsize := zoom * 10 div 100;
width := noofcellsx * cellsize;
height := noofcellsy * cellsize;
end;
In the form's unit I have arranged things up (through an ApplicationEvents object) so that SetZoom is called with some zoom value, whenever the numeric +/- keys are pressed. The idea behind all this was to have my custom control snap to the upper left corner of the form (with some predefined margin/borderwidth), and have the entire form automatically adjust its size whenever I zoom in or out of the custom control, but never extending beyond the screen limits. It's working, but only up to the point where the scrollbars must become visible: they never show up. Since this is an autoscrollable form, aren't they supposed to show up whenever a control inside the form (Grid in this case) gets larger than the constrained form and get out of the way when it gets smaller? I even tried some refactoring by moving SetZoom to the form's class, but to no avail. What am i missing here?
----------------- COMPILABLE CODE ADDED AFTERWARDS ------------------
The project file:
program MyApp;
uses
Forms,
Grid in 'Source\Grid.pas',
GridForm in 'Source\GridForm.pas' {GridFrm},
Main in 'Source\Main.pas' {MainFrm};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TMainFrm, MainFrm);
Application.Run;
end.
The Main.pas:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TMainFrm = class(TForm)
CreateNewFormButton: TButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure CreateNewFormButtonClick(Sender: TObject);
end;
var
MainFrm: TMainFrm;
implementation
{$R *.dfm}
uses
GridForm;
procedure TMainFrm.CreateNewFormButtonClick(Sender: TObject);
var aform : TForm;
begin
aform := TGridFrm.Create(self, point(15, 15));
aform.show;
tgridfrm(aform).grid.SetZoom(100);
end;
procedure TMainFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
end.
The GridForm.pas:
unit GridForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grid, AppEvnts;
type
TGridFrm = class(TForm)
ApplicationEvents1: TApplicationEvents;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
private
TheGrid : TGrid;
public
property Grid : TGrid READ TheGrid WRITE TheGrid;
constructor Create(AOwner : TComponent; ASize : TPoint);
end;
var
GridFrm: TGridFrm;
implementation
{$R *.dfm}
procedure TGridFrm.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var keystate : TKeyboardState;
begin
if not Active then begin exit; end;
if msg.message = WM_KEYDOWN then
begin
getkeyboardstate(keystate);
case msg.wparam of
vk_Add : begin // zoom in
grid.setzoom(grid.zoom + 10);
handled := True;
end;
vk_Subtract : begin // zoom out
grid.setzoom(grid.zoom - 10);
handled := True;
end;
// other keys down here...
end;
end;
end;
constructor TGridFrm.Create(AOwner : TComponent; ASize : TPoint);
begin
inherited Create(AOwner);
borderstyle := bsSingle;
borderwidth := 2;
autosize := True;
autoscroll := True;
constraints.maxwidth := screen.width - 1;
constraints.maxheight := screen.height - 1;
visible := False;
grid := TGrid.Create(asize.x, asize.y, random(800) + 500, self);
end;
procedure TGridFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
end.
And the Grid.pas:
unit Grid;
interface
uses
StdCtrls, SysUtils, Controls, Forms, Graphics, Dialogs;
type
TGrid = class (TCustomControl)
Lbl1, Lbl2,
GridSizeInfoLbl,
FormSizeInfoLbl,
WarningLbl : TLabel;
public
NoOfCellsX,
NoOfCellsY,
SquareSize, // in 1/1000ths of centimeter
CellSize, // in pixels
Zoom : integer;
procedure SetZoom(z : integer);
constructor Create(x, y, asquaresize : integer; AParent : TForm = nil);
end;
implementation
uses
GridForm;
constructor TGrid.Create(x, y, asquaresize : integer; AParent : TForm = nil);
begin
inherited Create(AParent);
parent := AParent;
color := clTeal;
align := alCustom;
left := 0;
top := 0;
noofcellsx := x;
noofcellsy := y;
squaresize := asquaresize;
Lbl1 := TLabel.Create(self);
Lbl2 := TLabel.Create(self);
GridSizeInfoLbl := TLabel.Create(self);
FormSizeInfoLbl := TLabel.Create(self);
WarningLbl := TLabel.Create(self);
with Lbl1 do
begin
parent := self;
caption := 'Size of grid: ';
width := 55;
height := 18;
left := 2;
top := 1;
end;
with Lbl2 do
begin
parent := self;
caption := 'Size of form: ';
width := 75;
height := 18;
left := 2;
top := 19;
end;
with GridSizeInfoLbl do
begin
parent := self;
width := 100;
height := 18;
left := 65;
top := 1;
end;
with FormSizeInfoLbl do
begin
parent := self;
width := 100;
height := 18;
left := 65;
top := 19;
end;
with WarningLbl do
begin
parent := self;
width := 150;
height := 18;
left := 2;
top := 39;
end;
end;
procedure TGrid.SetZoom(z : integer);
begin
zoom := z;
cellsize := (screen.pixelsperinch * squaresize * zoom) div (1000 * 254);
width := noofcellsx * cellsize;
height := noofcellsy * cellsize;
GridSizeInfoLbl.caption := inttostr(Width) +
'x' + inttostr(Height) +
' (zoom: ' + inttostr(zoom) +
', cellsize zoomed: ' + inttostr(cellsize) +
', squaresize: ' + inttostr(squaresize) +
'mm, squares: ' + inttostr(noofcellsx) + 'x' + inttostr(noofcellsy) + ')';
with tgridfrm(parent) do
begin
left := (screen.Width - width) div 2;
top := (screen.Height - height) div 2;
FormSizeInfoLbl.caption := inttostr(Width) + 'x' + inttostr(Height) +
' (clientarea: ' + inttostr(clientwidth) + 'x' + inttostr(clientheight) + ')';
if self.width > clientwidth then
if self.Height > clientheight then
warninglbl.caption := 'Both scrollbars should appear!'
else
warninglbl.caption := 'Horizontal scrollbar should appear!'
else if self.Height > clientheight then
warninglbl.caption := 'Vertical scrollbar should appear!'
else
warninglbl.caption := 'No scrollbars needed';
end;
end;
end.
Code synopsis: A click on the main form' s button creates an autosizeable form, which in turn creates a child grid of random initial size. Numeric +/- keys make the grid larger or smaller and the form is autosized accordingly, but no scrollbars ever show up, no matter how large the grid becomes (the labels I added provide visual feedback).
Your problem is twofold.
The first is, as Jerry commented to the question, AutoSize. The purpose of autosize is to resize the form such that content is visible. There can be no scrollbars when all content is visible, so clearly the two properties are contradictory.
As such VCL developers have took their precaution. Below is from D2007 source:
function TScrollingWinControl.AutoScrollEnabled: Boolean;
begin
Result := not AutoSize and not (DockSite and UseDockManager);
end;
As you can see setting AutoScroll has no effect when AutoSize is set.
You could override this behavior, this is a virtual method, if it wouldn't interfere with the second fold.
Now that you've decided to leave autosize off and calculate and set the required size of your form yourself depending on the workarea size, meet your second problem: alignment of your grid control.
The below is the D2007 code when a vertical scroll bar wants to see if it needs to adjust:
procedure ProcessVert(Control: TControl);
begin
if Control.Visible then
case Control.Align of
alTop, alNone:
if (Control.Align = alTop) or (Control.Anchors * [akTop, akBottom] = [akTop]) then
NewRange := Max(NewRange, Position + Control.Top + Control.Height);
alBottom: Inc(AlignMargin, Control.Height);
end;
end;
As you can see a control will not have an effect on an automatic vertical scroll bar if it doesn't have either alTop, alBottom or alNone alignment. Yours have alCustom.
This is also why overriding autosizing behavior won't help, AutoSize depends on controls having "left", "right", "top", "bottom" or "none" aligned controls.
You have to redesign your control taking into consideration how VCL internally works. Not all of the internal dependency aspects can be documented, so you have to use the source for this kind of enhanced development.

DBGRID with Row Height variable

I would like to show in a DBGRID as follows:
Imagine "Grid" as follows:
ID - DESCRIPTION
1 - Line 1 of the grid
2 - Line 2 of the grid
3 - Line 3 of the grid
Now, suppose the size of the DESCRIPTION column is changed and no longer appear the words "GRID";
I would like to stay as well DBGRID
ID - DESCRIPTION
1 - Line 1 of the
grid
2 - Line 2 of the
grid
3 - Line 3 of the
grid
is there any possibility that ??
Not what you're asking, but might help... I once used this code to show complete Memo fields in the standard DBGrid:
TMyForm = class(TForm)
...
private
FormMemoRect: TRect;
MemoGrid: TDBGrid;
BMemo: TBitBtn;
...
Procedure TMyForm.FormMemoDeactivate(Sender: TObject);
Begin
(Sender As TForm).Close;
Sender.Free;
End;
Procedure TMyForm.BMemoClick(Sender: TObject);
Var FormMemo: TForm;
Begin
MemoGrid.SetFocus;
FormMemo := TForm.Create(Self);
With TMemo.Create(FormMemo) Do Begin
Parent := FormMemo;
Align := alClient;
ReadOnly := True;
WordWrap := True;
ScrollBars := ssVertical;
Lines.Text := MemoGrid.DataSource.DataSet.Fields[TComponent(Sender).Tag].AsString;
End;
With FormMemo Do Begin
OnDeactivate := FormMemoDeactivate;
Left := FormMemoRect.Left;
Top := FormMemoRect.Top;
Width := Max(FormMemoRect.Right - FormMemoRect.Left, 300);
Height := FormMemoRect.Bottom - FormMemoRect.Top;
BorderStyle := bsNone;
Show;
End;
End;
Procedure TMyForm.GrdMemoDrawColumnCell(Sender: TObject; Const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
Begin
If (gdFocused In State) Then Begin
If Column.Field.DataType In [ftBlob, ftMemo] Then Begin
{Desenha botão para visualização do Memo}
FormMemoRect.Left := TWinControl(Sender).ClientToScreen(Rect.TopLeft).X;
FormMemoRect.Right := TWinControl(Sender).ClientToScreen(Rect.BottomRight).X;
FormMemoRect.Top := TWinControl(Sender).ClientToScreen(Rect.BottomRight).Y;
FormMemoRect.Bottom := FormMemoRect.Top + 100;
If Not Assigned(BMemo) Then
BMemo := TBitBtn.Create(Self);
BMemo.Parent := TWinControl(Sender).Parent;
BMemo.Width := 16;
BMemo.Height := 16;
BMemo.Caption := '...';
BMemo.OnClick := BMemoClick;
BMemo.Tag := Column.Field.Index;
BMemo.Left := TWinControl(Sender).Left + Rect.Right - BMemo.Width + 1;
BMemo.Top := TWinControl(Sender).Top + Rect.Top + 2;
MemoGrid := TDBGrid(Sender);
End
Else
FreeAndNil(BMemo);
End;
End;
For Blob/Memo Fields, you may also find it useful to do some custom GetText to show something directly in the Grid:
Procedure TMyForm.DataSetMemoGetText(Sender: TField; var Text: String; DisplayText: Boolean);
Begin
Text := Copy(Sender.AsString, 1, 50);
If Text <> Sender.AsString Then
Text := Text + '...';
End;
This is how the result looks like.
PS: Sorry for non-standard code style.

How to create a dialog like component that allows drop other controls inside it?

It is a Firemonkey component, however I could see that most of the component base is the same for VCL and FMX, so please if you know how to do that in VCL share your knowledge, it can be eventually the solution for my case.
I am using a TPopup as the ancestor. It is convenient for me since it remains on the form/frame and I can wire it with LiveBindings using the same context/structure of the parent, this is very convenient for me.
I need it behave exactly it is the TPopup, as a container. But I need it looks better and have my specific buttons (I have created some properties and automations for my software inside it)
The problem is that I create some internal controls, like TLayouts, Tpanels and Tbuttons to make looks like this: (empty)
That black area inside it is where I want to drop controls like TEdit and others.
I have set all the internal created controls to Store = false, so it is not getting stored on the streaming system. Doing that when I drop a TEdit for example, what I get is this (Tedit with aligned=top I need this):
However I was expecting this:
If I change the Store = true I can get the right effect, but all the inside controls are exposed on the Structure panel and every time I save the form and reopen everything gets duplicated. The inside components exposed is not a problem for me, but the duplication is, if I close and open the component 10 times I will get the entire inside structure replicated 10 time.
I will try to show some code that is related to the design of the component:
Class declaration:
[ComponentPlatformsAttribute(pidWin32 or pidWin64 or pidOSX32 or pidiOSSimulator or pidiOSDevice or pidAndroid)]
TNaharFMXPopup = class(TPopup, INaharControlAdapter, INaharControl)
private
protected
FpnlMain : TPanel;
FlytToolBar : TLayout;
FbtnClose : TButton;
FbtnSave : TButton;
FbtnEdit : TButton;
FpnlClientArea : TPanel;
FlblTitle : TLabel;
procedure Loaded; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
constructor Create:
constructor TNaharFMXPopup.Create(AOwner: TComponent);
begin
inherited;
FpnlMain := TPanel.Create(Self);
FlblTitle := TLabel.Create(Self);
FlytToolBar := TLayout.Create(Self);
FbtnEdit := TButton.Create(Self);
FpnlClientArea := TPanel.Create(Self);
FbtnClose := TButton.Create(FlytToolBar);
FbtnSave := TButton.Create(FlytToolBar);
Height := 382;
Placement := TPlacement.Center;
StyleLookup := 'combopopupstyle';
Width := 300;
ApplyControlsProp;
end;
Setting properties of the internal controls:
procedure TNaharFMXPopup.ApplyControlsProp;
begin
with FpnlMain do
begin
Parent := Self;
Align := TAlignLayout.Client;
StyleLookup := 'grouppanel';
TabOrder := 0;
Margins.Bottom := 10;
Margins.Left := 10;
Margins.Right := 10;
Margins.Top := 10;
Stored := false;
end;
with FlblTitle do
begin
Parent := FpnlMain;
Text := 'Título';
Align := TAlignLayout.Top;
Height := 36;
StyleLookup := 'flyouttitlelabel';
Stored := false;
end;
with FpnlClientArea do
begin
Parent := FpnlMain;
Align := TAlignLayout.Client;
StyleLookup := 'gridpanel';
TabOrder := 0;
Margins.Bottom := 5;
Margins.Left := 5;
Margins.Right := 5;
Margins.Top := 5;
Stored := false;
end;
with FlytToolBar do
begin
Parent := FpnlMain;
Align := TAlignLayout.Bottom;
Height := 50;
Stored := false;
end;
with FbtnClose do
begin
Parent := FlytToolBar;
Text := 'Fecha';
Align := TAlignLayout.Left;
Height := 50;
StyleLookup := 'tilebutton';
TabOrder := 0;
Width := 70;
ModalResult := mrClose;
Stored := false;
end;
with FbtnEdit do
begin
Parent := FlytToolBar;
Text := '';//'Edita';
Align := TAlignLayout.Left;
Height := 50;
StyleLookup := 'tilebutton';
TabOrder := 1;
Width := 70;
ModalResult := mrContinue;
Stored := false;
Enabled := false;
end;
with FbtnSave do
begin
Parent := FlytToolBar;
Text := 'Salva';
Align := TAlignLayout.Left;
Height := 50;
StyleLookup := 'tilebutton';
TabOrder := 2;
Width := 70;
ModalResult := mrOk;
Stored := false;
end;
end;
Loaded:
procedure TNaharFMXPopup.Loaded;
begin
inherited;
ApplyControlsProp;
SetEvents;
end;
I have tried the following with notification, trying to make the inserted control a parent for my intenal "clientarea"
procedure TNaharFMXPopup.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (Operation = opInsert) and (csDesigning in ComponentState) then
begin
if AComponent.Owner = self then
if AComponent is TFmxObject then
begin
(AComponent as TFmxObject).Parent := FpnlClientArea;
end;
end;
end;
But that made nothing change.
I have asked similar question before, but I was not aware of many things on creating such a component and the answer I got gave little help, I was missing the Parent of each internal component.
Now I am trying to really show where is my need: I need to drop controls on my TPopup dialog that will be parented of the ClientArea inside it.
Take a closer look at TTabControl / TTabItem in the unit FMX.TabControl. This is your perfect example because it basically needs to solve the same problem.
The following function is what you need to override:
procedure DoAddObject(const AObject: TFmxObject); override;
This is called when a control is added to your control. Override this function so that your control is added to the FpnlClientArea control instead. You'd get something similar to this:
procedure TNaharFMXPopup.DoAddObject(const AObject: TFmxObject);
// ...
begin
if (FpnlClientArea <> nil) and not AObject.Equals(FpnlClientArea) and not AObject.Equals(ResourceLink) then
begin
FpnlClientArea.AddObject(AObject);
end
else
inherited;
end;
Make sure that AObject.Equals also excludes your other "not stored" controls.
Without the DoAddObject override, the FMX TabControl would show the same problem as your component currently has.
The TPopup is not intended to accept controls. So that needs a few more tricks.
Here's a modified version of your unit that works for me. I've added a few comments:
unit NaharFMXPopup;
interface
uses
System.UITypes,
System.Variants,
System.SysUtils, System.Classes, FMX.Types, FMX.Controls, FMX.Layouts, FMX.StdCtrls;
type
[ComponentPlatformsAttribute(pidWin32 or pidWin64 or pidOSX32 or pidiOSSimulator or pidiOSDevice or pidAndroid)]
TNaharFMXPopup = class(TPopup)
private
procedure ApplyControlsProp;
protected
FpnlMain : TPanel;
FlytToolBar : TLayout;
FbtnClose : TButton;
FbtnSave : TButton;
FbtnEdit : TButton;
FpnlClientArea : TContent; // change to TContent.
// For TPanel we'd have to call SetAcceptControls(False),
// but that is not easily possible because that is protected
FlblTitle : TLabel;
procedure Loaded; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure DoAddObject(const AObject: TFmxObject); override;
public
procedure InternalOnClose(Sender: TObject);
procedure InternalOnSave(Sender: TObject);
procedure InternalOnEdit(Sender: TObject);
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetEvents;
published
end;
implementation
{ TNaharFMXPopup }
constructor TNaharFMXPopup.Create(AOwner: TComponent);
begin
inherited;
FpnlMain := TPanel.Create(Self);
FlblTitle := TLabel.Create(Self);
FlytToolBar := TLayout.Create(Self);
FbtnEdit := TButton.Create(Self);
FpnlClientArea := TContent.Create(Self); // change to TContent
FbtnClose := TButton.Create(FlytToolBar);
FbtnSave := TButton.Create(FlytToolBar);
Height := 382;
Placement := TPlacement.Center;
StyleLookup := 'combopopupstyle';
Width := 300;
// A TPopup is not intended to accept controls
// so we have to undo those restrictions:
Visible := True;
SetAcceptsControls(True);
ApplyControlsProp;
end;
destructor TNaharFMXPopup.Destroy;
begin
inherited;
end;
procedure TNaharFMXPopup.ApplyControlsProp;
begin
with FpnlMain do
begin
Parent := Self;
Align := TAlignLayout.Bottom;
StyleLookup := 'grouppanel';
TabOrder := 0;
Height := 50;
Margins.Bottom := 10;
Margins.Left := 10;
Margins.Right := 10;
Margins.Top := 10;
Stored := false;
end;
with FpnlClientArea do
begin
Parent := Self; // we have to change this to Self (it refuses working if the parent is FPnlMain)
Align := TAlignLayout.Client;
Margins.Left := 3;
Margins.Right := 3;
Margins.Top := 3;
Margins.Bottom := 3;
Stored := false;
end;
with FlytToolBar do
begin
Parent := FpnlMain;
Align := TAlignLayout.Bottom;
Height := 50;
Stored := false;
end;
with FbtnClose do
begin
Parent := FlytToolBar;
Text := 'Close';
Align := TAlignLayout.Left;
Height := 50;
StyleLookup := 'tilebutton';
TabOrder := 0;
Width := 70;
ModalResult := mrClose;
Stored := false;
end;
with FbtnEdit do
begin
Parent := FlytToolBar;
Text := '';//'Edita';
Align := TAlignLayout.Left;
Height := 50;
StyleLookup := 'tilebutton';
TabOrder := 1;
Width := 70;
ModalResult := mrContinue;
Stored := false;
Enabled := false;
end;
with FbtnSave do
begin
Parent := FlytToolBar;
Text := 'Save';
Align := TAlignLayout.Left;
Height := 50;
StyleLookup := 'tilebutton';
TabOrder := 2;
Width := 70;
ModalResult := mrOk;
Stored := false;
end;
end;
procedure TNaharFMXPopup.Loaded;
begin
inherited;
ApplyControlsProp;
// SetEvents;
end;
procedure TNaharFMXPopup.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
end;
procedure TNaharFMXPopup.InternalOnClose(Sender: TObject);
begin
end;
procedure TNaharFMXPopup.InternalOnEdit(Sender: TObject);
begin
end;
procedure TNaharFMXPopup.InternalOnSave(Sender: TObject);
begin
end;
procedure TNaharFMXPopup.SetEvents;
begin
FbtnClose.OnClick := InternalOnClose;
FbtnSave.OnClick := InternalOnSave;
FbtnEdit.OnClick := InternalOnEdit;
end;
procedure TNaharFMXPopup.DoAddObject(const AObject: TFmxObject);
begin
//inherited; try commenting the block bellow and uncommenting this one
//Exit;
if (FpnlClientArea <> nil)
and not AObject.Equals(FpnlClientArea)
and not AObject.Equals(ResourceLink)
and not AObject.Equals(FpnlMain)
and not AObject.Equals(FlblTitle)
and not AObject.Equals(FlytToolBar)
and not AObject.Equals(FbtnEdit)
and not AObject.Equals(FpnlClientArea)
and not AObject.Equals(FbtnClose)
and not AObject.Equals(FbtnSave) then
begin
FpnlClientArea.AddObject(AObject);
end
else
inherited;
end;
end.

Delphi XE6 firemonkey component alignment problems when added at runtime

i wanto dynamic add 5 TLable in my iOS app.
like this
Procedure Form1.FormCreate(Sender: TObject)
var
I: Integer;
begin
for I := 1 to 5 do
begin
with TLabel.Create(Self) do
begin
Parent := self;
Align := TAlignLayout.Top;
Height := 50;
Text := IntToStr(I);
end;
end;
end;
i think the order is 12345, but I get 15432.
What can I do to get the desired results?
You must give a chance to the aligning algorithm to do what you want.
procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
begin
for I := 1 to 5 do
begin
with TLabel.Create(Self) do
begin
Parent := self;
Align := TAlignLayout.alTop;
Height := 50;
Position.Y := I*Height; //add this line
Text := IntToStr(I);
end;
end;
end;

Resources