Autor Beitrag
Ares
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 128



BeitragVerfasst: Mi 23.12.09 14:10 
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:

ausblenden volle Höhe Delphi-Quelltext
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;

{ TMyComponent }

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited;

  FFlowPanel := TFlowPanel.Create(self);
  FFlowPanel.Parent := self ;
  FFlowPanel.Top := 0;
  FFlowPanel.Left := 0;
  FFlowPanel.AutoSize := true;
  //FFlowPanel.Width := 10;
  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:

ausblenden volle Höhe Delphi-Quelltext
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
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  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.Width := 10;
  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.

user defined image

Beim Buttonklick und in der Komponente werden alle Controls genau gleich erzeugt. Wie kommt also der Fehler zustande?

Besten Dank
Ares
Einloggen, um Attachments anzusehen!
Ares Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 128



BeitragVerfasst: Mi 23.12.09 14:32 
Ich habe gerade noch etwas herausgefunden:

Wenn man den Code zum erstellen der Controls auf dem "normalen" Panel nicht im Eventhandler des Buttonclick ausführt sondern direkt im FormCreate(), tritt dort auch der gleiche Fehler auf.

Es scheint also ein Unterschied zu sein, ob das Ganze beim Erzeugen des Formulars bzw. im Constructor ausgeführt wird, oder später z.B. nach einem Button-Klick. Aber warum? Warum könnte das eine Rolle spielen?
Ares Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 128



BeitragVerfasst: So 27.12.09 19:33 
Niemand eine Idee wo hier das Problem liegen könnte?