Einzelnen Beitrag anzeigen

Benutzerbild von haentschman
haentschman

Registriert seit: 24. Okt 2006
Ort: Seifhennersdorf / Sachsen
5.298 Beiträge
 
Delphi 12 Athens
 
#6

AW: Wie erstelle ich dynamische forms richtig ?

  Alt 6. Mär 2019, 08:40
Zitat:
TObjectList ist eine Liste von TObjects, wenn ich aus dieser Liste ein Object entfernte wird es automatisch freigegeben.
Richtig, wenn du es willst. Bei Nein verhällt sich die Liste wie eine TList. Du kannst nur über den Index auf das Objekt zugreifen.
Zitat:
TObjectDictionary überprüft automatisch ob ein TObject noch gebraucht wird oder nicht und gibt dies automatisch frei wenn es nicht mehr gebraucht wird.
Nicht ganz. TDictionary verhält sich, wenn man es möchte, wie eine TObjectList. ABER über den Key (string, Integer, TBlubb was auch immer) kannst du dir das zugehörige Objekt direkt holen.

Delphi-Quellcode:
unit Unit2;

interface

uses
  System.Classes, System.Generics.Collections, System.Generics.Defaults, System.SysUtils,
  Vcl.Forms, Vcl.Controls, Vcl.StdCtrls;
  form_child; // :-) entspricht nicht dem Styleguide...besser FormChild


type
  TForm2 = class(TForm)
    btnCreateChild: TButton;
    lblList: TLabel;
    lblDict: TLabel;
    lblDict2: TLabel;
    procedure btnCreateChildClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
   private
      FList: TObjectList<TForm>;
      FDictionary: TObjectDictionary<string, TForm>;
   public

   end;

var
   Form2: TForm2;

implementation

procedure TForm2.FormCreate(Sender: TObject);
begin
  FList := TObjectList<TForm>.Create(True); // True gibt die Objekte selbstständig frei
  FDictionary := TObjectDictionary<string, TForm>.Create([doOwnsValues]); // doOwnValues gibt die Objekte (Value) selbstständig frei
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  FList.Free;
  FDictionary.Free;
end;

procedure TForm2.btnCreateChildClick(Sender: TObject);
var
  I: Integer;
  Child: TForm;
  TempForm: TForm;
begin
  for I := 0 to 2 do
  begin
    Child := Tfrm_child.Create(nil); // :-) entspricht nicht dem Styleguide...besser TfrmChild
    Child.Name := 'Child' + IntToStr(I);
    Child.Align := alLeft;
    Child.Parent := cxScrollbox;
    Child.AlignWithMargins := True;
    Child.pnlchild.Color := clRed;
    Child.pnlchild.Caption := 'ArrayINDEX = ' + IntToStr(Integer);

    FList.Add(Child);
    FDictionary.Add(Child.Name, Child);

    Child.Show;
  end;


  lblList.Caption := 'Name des ersten Eintrages: ' + FList[0].Name;
  lblDict.Caption := 'Name des 2. Eintrages im Dictionary: ' + FDictionary.Items['Child2'].Name;

  // Wenn man nicht weiß ob der Schlüssel existiert
  FDictionary.TryGetValue('Child2', TempForm);
  if Assigned(TempForm) then
  begin
    lblDict.Caption := 'Name des 3. Eintrages im Dictionary: ' + TempForm.Name;
  end
  else
  begin
    lblDict.Caption := 'Name des 3. Eintrages im Dictionary: nicht gefunden';
  end;
end;

end.
Delphi-Quellcode:
object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 299
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object lblList: TLabel
    Left = 48
    Top = 96
    Width = 26
    Height = 13
    Caption = 'lblList'
  end
  object lblDict: TLabel
    Left = 48
    Top = 120
    Width = 28
    Height = 13
    Caption = 'lblDict'
  end
  object lblDict2: TLabel
    Left = 48
    Top = 144
    Width = 34
    Height = 13
    Caption = 'lblDict2'
  end
  object btnCreateChild: TButton
    Left = 40
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Create'
    TabOrder = 0
    OnClick = btnCreateChildClick
  end
end

Geändert von haentschman ( 6. Mär 2019 um 08:49 Uhr)
  Mit Zitat antworten Zitat