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 TPagecontrol klonen - irgendwie unsichtbar (https://www.delphipraxis.net/76819-tpagecontrol-klonen-irgendwie-unsichtbar.html)

API 10. Sep 2006 15:13


TPagecontrol klonen - irgendwie unsichtbar
 
Liste der Anhänge anzeigen (Anzahl: 1)
:hi:

Versuche gerade, ein TPagecontrol (2 TTabsheets + Controls darauf) zu klonen.
Das Klonen funktioniert soweit, nur die Controls auf den TTabsheets sind nicht sichtbar..

Habe schon versucht, die geklonten Controls sichtbar zu machen (visible = True). Bringt aber nichts.
Auch liegen die Controls IMO innerhalb des sichtbaren Bereichs...Was läuft hier falsch?

Anbei ein Demoprojekt zum selber nachvollziehen.

Nachtrag: Selbt wenn ich nach dem Klonen z.B einen TButton auf eines der neu erstellten TTabsheets
setze, wird es nicht angezeigt...

Codeauszug:
Delphi-Quellcode:
function CloneComponent(Source: TComponent;
  aInstance: TComponent = nil;
  aOwner: TComponent = nil;
  aParent: TWinControl = nil;
  Options: TCloneOptions = []): TComponent;
const
  Index: Integer = 0; // static, needs $J+ state
var
  I: Integer;
  OrgName: string;
  Stream: TMemoryStream;
begin
  Result := nil;
  if not Assigned(Source) then Exit;
  // Create new instance when needed (or just use the one given).
  if aInstance = nil then
  begin
    if aOwner = nil then
      aOwner := Source.Owner;
    Result := TComponentClass(Source.ClassType).Create(aOwner)
  end
  else
    if aInstance.ClassType = Source.ClassType then
      Result := aInstance
    else
      raise Exception.Create('<aInstance> = ' + aInstance.ClassName +
        ' is not the same component class as' +
        ' <Source> = ' + Source.ClassName);


  // Although registering of normal classes shouldn't be neccessary
  // it is required for some of the children like TToolButton, etc.
  if Source is TWinControl then
    with TWinControl(Source) do
      for I := 0 to ControlCount - 1 do
      begin
        try
          RegisterClass(TPersistentClass(Controls[I].ClassType));
        except
        end;
      end;
  try
    // Use new parent when one was given, otherwise use the one from <Source>.
    if Source is TControl then
      with TControl(Result) do
        if aParent <> nil then
          Parent := aParent
        else
          Parent := TControl(Source).Parent;


    // Clone published properties.
    OrgName := Source.Name;
    Stream := TMemoryStream.Create;
    try
      Source.Name := 'CloneComponent' + IntToStr(Index) + OrgName;
      Stream.WriteComponent(Source);
      Source.Name := OrgName;
      Stream.Position := 0;
      Result := Stream.ReadComponent(Result);
    finally
      Stream.Free;
    end;
    // Try to set caption/text when <Source> is a control
    // to the same caption/text as that of the <Source> control.
    if Source is TControl then
      if IsPublishedProp(Result, 'Caption') or
        IsPublishedProp(Result, 'Text') then
        with TControlCracker(Result) do
          if (csSetCaption in ControlStyle) and (Name = Text) then
            Text := OrgName;

  except
    // If something went wrong only free the clone
    // when it was created by this function.
    if aInstance = nil then Result.Free;
    raise;
  end;

  // Clone all Children to the new instance as well?
  if (coIncludeChildren in Options) and (Source is TWinControl) then
    with TWinControl(Source) do
      for I := 0 to ControlCount - 1 do
        if Controls[I].ClassName <> 'TToolButton' then
          CloneComponent(Controls[I], nil, nil, TWinControl(Result), Options);

  if Index < MAXINT then Inc(Index) else Index := 0;
end;

marabu 10. Sep 2006 18:11

Re: TPagecontrol klonen
 
Hallo,

nicht dass ich mich mit diesem Problem eingehend beschäftigt hätte, aber eine allgemeine Lösung scheint das trotz des Aufwandes nicht zu sein. WriteComponent() und ReadComponent() verarbeiten grundsätzlich auch child components - unabhängig von der Option coIncludeChildren. Und mit dem PageControl (und einigen anderen Komponenten) dürfte es auf jeden Fall ein Problem geben, wenn Tabsheets behandelt werden, als wären sie mit herkömmlichen Komponenten (TButton, TEdit, etc.) vergleichbar. Der original author ist wohl selbst nicht von der Universalität seines Codes überzeugt gewesen - siehe TToolButton.

Grüße vom marabu

API 10. Sep 2006 20:17

Re: TPagecontrol klonen
 
Dann müsste ich wohl die Tabsheets einzeln klonen.
D.h ein neues Pagecontrol mit den Tabsheets erstellen (per Create Methode)
und dann die Controls einzeln klonen.

API 10. Sep 2006 21:19

Re: TPagecontrol klonen
 
Zitat:

Zitat von marabu
WriteComponent() und ReadComponent() verarbeiten grundsätzlich auch child components - unabhängig von der Option coIncludeChildren.

stimmt

Zitat:

Zitat von marabu
Und mit dem PageControl (und einigen anderen Komponenten) dürfte es auf jeden Fall ein Problem geben, wenn Tabsheets behandelt werden, als wären sie mit herkömmlichen Komponenten (TButton, TEdit, etc.) vergleichbar.

Seltsam ist aber, dass das Memo + Button auf dem Tabsheet geklont werden, d.h sie existieren tatsächlich
auf den geklonten Tabsheets, nur irgendwie unsichtbar...


Der Beweis (durch Auflistung aller Controls)

Zitat:

Memo1
frmKlon
Button6
Button5
PageControl1
TabSheet2
Memo1
TabSheet1
Button2
Button4
Panel3
Panel1
Memo2
Button3
Panel2
CloneComponent0PageControl1
CloneComponent2TabSheet2
CloneComponent2Memo1
CloneComponent0TabSheet1
CloneComponent0Button2
TabSheet2
TabSheet1
Button1

API 12. Sep 2006 08:12

Re: TPagecontrol klonen - irgendwie unsichtbar
 
*PUSH*

Hat sonst niemand eine Idee?
habe ja extra ein Beispiel Projekt gemacht, welches nur 3 Personen heruntergeladen haben....;(


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:10 Uhr.

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