Autor Beitrag
ensacom1
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 08.12.08 14:14 
Hallo, ich benutze folgenden Code um Frames zur Laufzeit in meine Form zu laden

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:
private
    FActiveFrame:TFrame; //zum merken des derzeit aktiven Frames



uses  
  UnitFrameShowInteressenten, UnitFrameObjekte;




procedure TForm1.Button1Click(Sender: TObject);
begin
  if (not assigned(FActiveFrame)) or not (FActiveFrame is TFrameShowInteressenten) then
  begin
    freeandnil(FActiveFrame);
    FActiveFrame:=TFrameShowInteressenten.Create(self);
    FActiveFrame.Parent:=TabSheet1;
    TabSheet1.Caption := 'Interessenten';
  end;
end;




procedure TForm1.Button2Click(Sender: TObject);
begin
  if (not assigned(FActiveFrame)) or not (FActiveFrame is TFrameObjekte) then
  begin
    freeandnil(FActiveFrame);
    FActiveFrame:=TFrameObjekte.Create(self);
    FActiveFrame.Parent:=TabSheet1;
    TabSheet1.Caption := 'Objekte';
  end;
end;




Kann mir nun einer sagen wie ich das ganze so aufrufen kann, dass ich nur den Namen des Frames angeben brauche?

Danke
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 08.12.08 14:25 
Hallo und :welcome: bei uns ;-)

Du kannst FindComponent benutzen, dann musst du aber die Eigenschaft Name auch zuweisen, sonst wird ein Standardname benutzt.
ensacom1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 08.12.08 14:33 
hm, wie meinst Du das?
Wie mache ich dann die Abfrage?
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 08.12.08 14:35 
ausblenden Delphi-Quelltext
1:
2:
3:
FActiveFrame.Name := 'XYZ';

(FindComponent('XYZ') as TFrameShowInteressenten).
ensacom1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 08.12.08 14:43 
also, ich meine das so.

Ich habe jetzt ca 8 Frames.
aus einer ComboBox will ich den Namen eines Frames auswählen und dieses soll dann angezeigt werden.

Mit meinem code komme ich da nicht wirklich weiter.
ensacom1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 08.12.08 15:01 
Also was ich will ist einfach nur, dass ich in den unten fett geschriebenen Stellen den Namen der Frames als String übergeben kann und nicht wie jetzt als TFrame

if (not assigned(FActiveFrame)) or not (FActiveFrame is TfrGewerbeMaskeAngebot) then
begin
freeandnil(FActiveFrame);
FActiveFrame:=TfrGewerbeMaskeAngebot.Create(self);
FActiveFrame.Parent := pnlFrameParent;
end;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 08.12.08 15:21 
Aaah, jetzt hab ich das verstanden. Das müsste mit TClass / class of gehen.

Die erste Abfrage sollte auch mit FActiveFrame.ClassName = 'TfrGewerbeMaskeAngebot' gehen. Das geht aber natürlich nur mit der letzten Klasse in der Vererbungshierarchie, mit is kann man ja jeden Vorfahren auch abfragen.
ensacom1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 08.12.08 15:31 
Ok, mit der ersten Abfrage so wie du es schreibst geht es schon mal ohne das ein Fehler kommt.
Aber wie übergebe ich nun den string dass das Frame dessen Name in s steht auch erzeugt wird?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TfNewInteressent.Button1Click(Sender: TObject);
var s : string;
begin
  s := 'TfrGewerbeMaskeAngebot';

  if (not assigned(FActiveFrame)) or not (FActiveFrame.ClassName = s) then
  begin
    freeandnil(FActiveFrame);
    FActiveFrame := TfrGewerbeMaskeAngebot.Create(self); <--- Hier will ich auch nur s übergben und das Frame soll erstellt wredem
    FActiveFrame.Parent := pnlFrameParent;
  end;
end;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 08.12.08 15:43 
Dafür würde ich eine Factory-Klasse benutzen, die die Klassen kennt und diese erzeugt.
Anders habe ich es nie ausprobiert, weil dieses Pattern sehr angenehm ist.

Ansonsten:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
type
  TMyParentClass = class of TControl;
var
  MyClass: TMyParentClass;
  NewObject: TControl;
begin
  MyClass := TRadioButton;
  NewObject := MyClass.Create(Self);
  NewObject.Parent := Self;
  TRadioButton(NewObject).Caption := 'Test';
Irgendwo musst du die Klassen trotzdem z.B. in eine Liste eintragen.
ensacom1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 08.12.08 15:46 
Erst mal danke für das Beispiel.

Was meinst Du mit "Ich muss die Klassen trotzdem in eine Liste eintragen"?
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 08.12.08 15:48 
Direkt aus dem String kannst du nichts machen, du könntest ein konstantes Array benutzen oder so, auf das du z.B. mit konstanten IDs zugreifen kannst o.ä., aber eine direkte Umwandlung des Strings geht nur über eine Skriptsprache.
ensacom1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 08.12.08 15:52 
über ein array wäre auch ok.
Hm icvh denke ich bekomme das nicht mehr hin. Muss ich wohl doch einen anderen Weg probieren
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 08.12.08 15:59 
Anderes Beispiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure ...
type
  TMyParentClass = class of TControl;
  TClassListID = (cliRadioButton = 0, cliCheckBox = 1, cliButton = 2);
const
  MyClassList: array[0..2] of TMyParentClass = (TRadioButton, TCheckBox, TButton);

  procedure AddNewControl(uControlType: TMyParentClass);
  var
    NewObject: TControl;
  begin
    NewObject := uControlType.Create(Self);
    NewObject.Parent := Self;
  end;

begin
  AddNewControl(MyClassList[Integer(cliCheckBox)]);
Nico72
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 50

Win XP, Vista
Delphi 7, Delphi 2006
BeitragVerfasst: Mo 08.12.08 16:04 
Hallo,

das sollte doch mit GetClass funktionieren...
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 08.12.08 16:06 
Richtig, das kannte ich noch gar nicht. Wieder was neues gelernt. ;-)
www.mindspring.com/~...oo/tips/getclass.txt
ensacom1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 08.12.08 16:28 
Ja genau, ich habe eben diesen Tipp von
Hawkeye219 aus dem Delphi-Praxis-Forum bekommen und es läuft super.

Hallo,

die Umwandlung eines Strings in eine Klassenreferenz kann mit Hilfe der Funktion GetClass durchgeführt werden. Die Frame-Klassen müssen dazu einmalig mittels Schau in die Delphi-HilfeRegisterClass registriert werden.

ausblenden 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:
type
  TFrameClass = class of TFrame;
 
function FindFrameClass (const aClassName: string; var aFrameClass: TFrameClass): Boolean;
var
  ClassRef : TPersistentClass;
begin
  ClassRef := GetClass(aClassName);
  Result := Assigned(ClassRef) and ClassRef.InheritsFrom(TFrame);
  if Result then
    aFrameClass := TFrameClass(ClassRef);
end;
 
// Beispiel für den Einsatz der Funktion: 
 
var
  FrameClass : TFrameClass;
begin
  RegisterClass (TFrameShowInteressenten);
  RegisterClass (TFrameObjekte);
  RegisterClass (TfrGewerbeMaskeAngebot);

  if FindFrameClass('TfrGewerbeMaskeAngebot', FrameClass) then
    if (not Assigned(FActiveFrame)) or not (FActiveFrame is FrameClass) then
      begin
        FreeAndNil (FActiveFrame);
        FActiveFrame:= FrameClass.Create(Self);
        FActiveFrame.Parent := pnlFrameParent;
      end;
end;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 08.12.08 16:30 
Es ist übrigens in beiden Foren unerwünscht Cross-Posts nicht anzugeben. Wenn man die verlinkt kann man ggf. schauen ob es bereits Antworten im anderen Forum gibt. ;-)