1

This is a follow up of this question: Moving controls between Delphi components

In design time I put elements into my custom TPanel but when I write them to the DFM I change their names

procedure TPanelDialogo.VolcarFrameEnLista( );
var
  i: integer;
  Componente: TControl;
begin
  // recorrer el frame y rescatar sus componentes
  if FDesignPanel = nil then
    exit;
  for i := FDesignPanel.ControlCount - 1 downto 0 do
  begin
    Componente := FDesignPanel.Controls[i];
    if Pos( self.Name + '_', Componente.Name ) = 0 then
    begin
      Componente.Name := self.Name + '_' + Componente.Name;
    end;
    if FListaComponentes.IndexOf(Componente) < 0 then
    begin
      FListaComponentes.Add( Componente );
    end;
  end;
end;

procedure TPanelDialogo.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
  i: integer;
  OwnedComponent: TComponent;
begin
  if FDesignPanel <> nil then
    VolcarFrameEnLista();

  for i := 0 to self.FListaComponentes.Count - 1 do
  begin
    OwnedComponent := FListaComponentes.Items[i];
    Proc(OwnedComponent);
  end;
end;

In design time, when I put a label into the custom TPanel this label is added to the form in the .pas file:

Label in pas file

When writing to the DFM file I rename the label, as seen before so the declaration in the .pas file is no longer valid.

This is the .pas file

type
  TForm1 = class(TForm)
    CRTTESTPANEL: TGENPant;
    PanelDialogo1: TPanelDialogo;
    Label1: TLabel;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

And this is the DFM

object Form1: TForm1
  ...
  object CRTTESTPANEL: TGENPant
    ...
  end
  object PanelDialogo1: TPanelDialogo
    ...
    object PanelDialogo1_Label1: TLabel
      ...
    end
  end
end

So I get this error message: Error message

And, after clicking "Yes" these are the DFM and pas files:

  TForm1 = class(TForm)
    CRTTESTPANEL: TGENPant;
    PanelDialogo1: TPanelDialogo;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
object Form1: TForm1
  ...
  object CRTTESTPANEL: TGENPant
  end
  object PanelDialogo1: TPanelDialogo
    ...
    object PanelDialogo1_Label1: TLabel
      ...
    end
  end
end

I'd like to avoid writing the label declaration to the .pas file. I've seen this question but it relates to writing to the DFM file, not the pas.

Is there a way to do what I want?

Héctor C.
  • 433
  • 4
  • 17
  • 1
    "*when I write them to the DFM I change their names*" - why? You should not be doing that at all. "*I'd like to avoid writing the label declaration to the .pas*" - you can't. The user dropped a component at design-time, it is owned by the Form, not your component. Your component is the Parent, not the Owner – Remy Lebeau Jun 07 '19 at 16:00
  • In the olden times (Delphi 3) you could clear the name property of a component. It would result in a component that did not have a variable declaration in the pas file but was still available in the dfm file. It had some side effects though: If you did not have any component of that type left on the form, sometimes you got runtime errors because the streaming code was not added to the form. Don't know whether that is still possible or solves your problem at all. – dummzeuch Jun 07 '19 at 17:39

1 Answers1

1

When you create Label1 at design time, Delphi automatically adds it to the tForm interface. But there is no reason you have to leave it there. You can delete it. It's just there for convenience so you can reference it from your code. If your code makes no references to the identifier Label1, you can delete it from the interface in the .PAS file.

David Dubois
  • 3,842
  • 3
  • 18
  • 36