Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Komponente zur laufzeit erzeugen Komp-liste anzeigen lassen (https://www.delphipraxis.net/139616-komponente-zur-laufzeit-erzeugen-komp-liste-anzeigen-lassen.html)

mentos112 1. Sep 2009 16:44


Komponente zur laufzeit erzeugen Komp-liste anzeigen lassen
 
Moin Leute,

habe wiedermal ein Problem vor mir liegen wo ich nicht weiter komme! Hoffe sehr das Ihr mir bei der Lösung dieses Problems helft!

Ich erzeuge zur Laufzeit von einen Formular auf einem anderen Komponenten. klappt soweit alles ganz gut, bis auf die tatsache, dass die erzeugten komponenten nicht in der Komponentenliste des 2.Formulars stehen. geb euch mal kurzen quellauszug

1. Formular
Delphi-Quellcode:
procedure TForm1.button1Click(Sender: TObject);
var  LEdit: TLabeledEdit;
      g_box: TGroupBox;
      radio: TRadioGroup;
      datum: TDateTimePicker;
      name: TLabel;
      button: TButton;
      i: integer;
begin

for i:=1 to 3 do begin

   Form6.Width := i*270+(i*10)+20;
   Form6.Height := 580;
   Form6.Position := poScreenCenter;

   g_box := TGroupBox.Create(Self);
   with g_box do
      begin
         Name := 'g_box_' + IntToStr(i);
         Caption := 'test ' + IntToStr(i);
         Parent := Form6;
         Height := 190;
         Width := 270;
         Left := (i-1)*280+10;
         Top := 10;
         ComponentIndex := 3+i;
      end;

   radio := TRadioGroup.Create(Self);
   with radio do
      begin
         Parent := g_box;
         Columns := 2;
         Items.Add('ware1');
         Items.Add('ware2');
         Height := 33;
         Width := 249;
         Left := 10;
         Top := 15;
      end;

   LEdit := TLabeledEdit.Create(Self);
   with LEdit do
      begin
         Name := 'W_Nr_' + IntToStr(i);
         EditLabel.Caption := 'Warennummer';
         Text := '';
         Parent := g_box;
         Width := 121;
         Top := 68;
         Left := 10;
      end;

   LEdit := TLabeledEdit.Create(Self);
   with LEdit do
      begin
         Name := 'Wert_' + IntToStr(i);
         EditLabel.Caption := 'Wert';
         Text := '';
         Parent := g_box;
         Width := 249;
         Top := 112;
         Left := 10;
      end;

   LEdit := TLabeledEdit.Create(Self);
   with LEdit do
      begin
         Name := 'Anzahl_' + IntToStr(i);
         EditLabel.Caption := 'Anzahl';
         Text := '';
         Parent := g_box;
         Width := 121;
         Top := 156;
         Left := 10;
      end;

   datum := TDateTimePicker.Create(Self);
   with datum do
      begin
         Name := 'datum_' + IntToStr(i);
         Parent := g_box;
         Width := 121;
         Top := 68;
         Left := 138;
      end;

   name := TLabel.Create(Self);
   with name do
      begin
         Name := 'label_' + IntToStr(i);
         Caption := 'Datum';
         Parent := g_box;
         Width := 121;
         Top := 52;
         Left := 138;
      end;

   button := TButton.Create(Self);
   with button do
      begin
         Name := 'lbutton_' + IntToStr(i);
         Caption := 'Laden';
         Parent := g_box;
         Width := 121;
         Top := 156;
         Left := 138;
      end;
end;

   button := TButton.Create(Self);
   with button do
      begin
         Name := 'sbutton';
         Caption := 'Speichern';
         Parent := Form6;
         Width := 81;
         Top := 210;
         Left := Form6.Width-273;
      end;

   button := TButton.Create(Self);
   with button do
      begin
         Name := 'dbutton';
         Caption := 'Drucken';
         Parent := Form6;
         Width := 81;
         Top := 210;
         Left := Form6.Width-187;
      end;

   button := TButton.Create(Self);
   with button do
      begin
         Name := 'bbutton';
         Caption := 'Schließen';
         Parent := Form6;
         Width := 81;
         Top := 210;
         Left := Form6.Width-101;
      end;

   Form6.ShowModal();
end;
2.Formular
Delphi-Quellcode:
procedure TForm6.Button1Click(Sender: TObject);
var  i: Integer;
begin
   Memo1.Lines.Clear;
   for i:= 0 to ComponentCount-1 do
   begin
      Memo1.Lines.Add(Components[i].name);
   end;
end;
Bekomme aber in dem Memo nur die Komponenten "MEMO" und "BUTTON" angezeigt, welche aber schon zur entwicklung erzeugt wurden!

Bitte um Eure Hilfe

Mit freundlichen Grüßen

mentos112

Apollonius 1. Sep 2009 16:46

Re: Komponente zur laufzeit erzeugen Komp-liste anzeigen las
 
Du musst beim Konstruktor-Aufruf das andere Formular als Owner angeben. Beim Owner tragen sich die Komponenten in der Liste ein.

mentos112 1. Sep 2009 16:50

Re: Komponente zur laufzeit erzeugen Komp-liste anzeigen las
 
Delphi-Quellcode:
g_box := TGroupBox.Create(Owner);
   with g_box do
      begin
         Name := 'g_box_' + IntToStr(i);
         Caption := 'Einsatz ' + IntToStr(i);
         Parent := Form6;
         Height := 190;
         Width := 270;
         Left := (i-1)*280+10;
         Top := 10;
         ComponentIndex := 3+i;
      end;
du meinst doch hier oder?

Das geht trotzdem nicht muss ich das noch irgendwie dem anderen form zu wissen geben?

Andreas L. 1. Sep 2009 17:01

Re: Komponente zur laufzeit erzeugen Komp-liste anzeigen las
 
Du kannst nicht mehrere Buttons in eine Variable schreiben.
Delphi-Quellcode:
   button := TButton.Create(Self);
   with button do
      begin
         Name := 'dbutton';
         Caption := 'Drucken';
         Parent := Form6;
         Width := 81;
         Top := 210;
         Left := Form6.Width-187;
      end;

   button := TButton.Create(Self);
   with button do
      begin
         Name := 'bbutton';
         Caption := 'Schließen';
         Parent := Form6;
         Width := 81;
         Top := 210;
         Left := Form6.Width-101;
      end;
  ...
Einfach für jeden Button eine Variable deklarieren.

EDIT: oder gleich so:
Delphi-Quellcode:
  with TButton.Create(Self) do
  begin
    Name := 'Button1';
  end;
EDIT2: Apollonius meinte das so:
Delphi-Quellcode:
g_box := TGroupBox.Create(NameDeinerAnderenForm);

Phoenix 1. Sep 2009 17:03

Re: Komponente zur laufzeit erzeugen Komp-liste anzeigen las
 
Der Owner (also das neue Form6, was Du ja auch als Parent angibst) muss im Konstruktor übergeben werden.
Also so:
Delphi-Quellcode:
g_box := TGroupBox.Create(Form6);


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:41 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz