1

这是这个问题的后续: 在 Delphi 组件之间移动控件

在设计时,我将元素放入我的自定义 TPanel,但是当我将它们写入 DFM 时,我更改了它们的名称

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;

在设计时,当我将标签放入自定义 TPanel 时,此标签会添加到 .pas 文件中的表单中:

pas文件中的标签

写入 DFM 文件时,我重命名了标签,如前所述,因此 .pas 文件中的声明不再有效。

这是 .pas 文件

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

这就是 DFM

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

所以我收到此错误消息: 错误信息

并且,单击“是”后,这些是 DFM 和 pas 文件:

  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

我想避免将标签声明写入 .pas 文件。我见过这个问题,但它与写入 DFM 文件有关,而不是 pas。

有没有办法做我想做的事?

4

1 回答 1

1

当您Label1在设计时创建时,Delphi 会自动将其添加到tForm界面中。但是没有理由你必须把它留在那里。你可以删除它。它只是为了方便起见,因此您可以从代码中引用它。如果你的代码没有引用标识符Label1,你可以从文件的接口中删除它.PAS

于 2019-06-08T11:05:00.187 回答