Hallo!
Ich versuche mich gerade daran eine eigene Komponente zu erstellen. Als Basis soll ein TPlanel dienen, dass verschiedene weitere Controls aufnimmt. Darunter auch ein TFlowPanel in dem dann weitere Controls angeordnet werden. Hierbei habe ich das Problem, dass das FlowPanel in meiner Komponente nicht so arbeitet wie es soll. Die Controls im FlowPanel werden gemäß dem gewählten FlowStyle "fsLeftRightTopBottom" positioniert, per AutoSize setzt das FlowPanel seine Größe aber so, als ob ob "fsTopBottomLeftRight" gewählt wäre.
Die Komponente:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63:
| unit MyComponent;
interface
uses SysUtils, Classes, Controls, ExtCtrls, StdCtrls, Forms, Graphics, Types, Messages, Windows;
type TMyComponent = class(TPanel) protected FLabel: TLabel; FFlowPanel : TFlowPanel; FPanel: TPanel; public constructor Create(AOwner: TComponent); override; published end;
procedure Register;
implementation
procedure Register; begin RegisterComponents('Test Components', [TMyComponent]); end;
constructor TMyComponent.Create(AOwner: TComponent); begin inherited;
FFlowPanel := TFlowPanel.Create(self); FFlowPanel.Parent := self ; FFlowPanel.Top := 0; FFlowPanel.Left := 0; FFlowPanel.AutoSize := true; FFlowPanel.Visible := true; FFlowPanel.ParentBackground := false; FFlowPanel.ParentColor := false; FFlowPanel.Color := clRed; FFlowPanel.FlowStyle := fsLeftRightTopBottom;
FLabel := TLabel.Create(FFlowPanel); FLabel.Parent := FFlowPanel; FLabel.Top := 10; FLabel.Left := 10; FLabel.Transparent := false; FLabel.Color := clYellow; FLabel.Caption := 'test';
FPanel := TPanel.Create(FFlowPanel); FPanel.Parent := FFlowPanel; FPanel.Width := 10; FPanel.Height := 50; FPanel.ParentBackground := false; FPanel.Color := clBlue; end; end. |
Wenn ich das Ganze ohne Komponente simuliere, also einfach ein Panel auf die Form setzte und darin ein TFlowPanel mit den gleichen Controls erzeuge wie in der Komponente tritt der Fehler nicht auf:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62:
| unit main;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, MyComponent, StdCtrls;
type TForm1 = class(TForm) Panel1: TPanel; Button1: TButton; MyComponent1: TMyComponent; procedure Button1Click(Sender: TObject); private public end;
var Form1: TForm1;
FLabel: TLabel; FFlowPanel : TFlowPanel; FPanel: TPanel;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); begin FFlowPanel := TFlowPanel.Create(Panel1); FFlowPanel.Parent := Panel1 ; FFlowPanel.Top := 0; FFlowPanel.Left := 0; FFlowPanel.AutoSize := true; FFlowPanel.Visible := true; FFlowPanel.ParentBackground := false; FFlowPanel.ParentColor := false; FFlowPanel.Color := clRed; FFlowPanel.FlowStyle := fsLeftRightTopBottom;
FLabel := TLabel.Create(FFlowPanel); FLabel.Parent := FFlowPanel; FLabel.Top := 10; FLabel.Left := 10; FLabel.Transparent := false; FLabel.Color := clYellow; FLabel.Caption := 'test';
FPanel := TPanel.Create(FFlowPanel); FPanel.Parent := FFlowPanel; FPanel.Width := 10; FPanel.Height := 50; FPanel.ParentBackground := false; FPanel.Color := clBlue; end;
end. |
Das Ergebnis ist in dem beigefügten Bild zu sehen. Links auf der Form ist das "normale" Panel in das das FlowPanel mit den Unter-Controls per Buttonklick eingefügt wurde. Rechts die Komponente mit dem Fehler im FlowPanel.
Beim Buttonklick und in der Komponente werden alle Controls genau gleich erzeugt. Wie kommt also der Fehler zustande?
Besten Dank
Ares