gpt4 book ai didi

delphi - 使用 TForm 作为组件基础时出现资源未找到错误

转载 作者:行者123 更新时间:2023-12-03 15:44:45 25 4
gpt4 key购买 nike

我正在编写一个组件,并希望将基本类型更改为 TForm,但是在运行时我收到错误“找不到资源 TMyComp”。我猜这是因为没有 dfm,但我不知道该怎么办。

谢谢

unit Unit65;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
TMyComp = class(TForm);

TForm65 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
Mc: TMyComp;
{ Private declarations }
public
{ Public declarations }
end;

var
Form65: TForm65;

implementation

{$R *.dfm}

procedure TForm65.Button1Click(Sender: TObject);
begin
Mc := TMyComp.Create(Self);
Mc.Parent := nil;
Mc.ShowModal;
end;

end.

最佳答案

TMyComp 没有 .dfm 文件。您可以通过调用CreateNew来避免尝试加载.dfm构造函数而不是Create

Mc := TMyComp.CreateNew(Self);

来自documentation :

Use CreateNew instead of Create to create a form without using the associated .DFM file to initialize it. Always use CreateNew if the TCustomForm descendant is not a TForm object or a descendant of TForm.

CreateNew bypasses the streaming in of the previously-associated .DFM file. If the form contains visual components, therefore, you must stream in an external .DFM to bind the visual components with their classes. If the newly created form has an external .DFM file, then you can follow the call to CreateNew with a call to InitInheritedComponent. If you need to create the .dfm file for the new form instance, bracket the call to CreateNew with calls to WriteComponentResFile and ReadComponentResFile.

关于delphi - 使用 TForm 作为组件基础时出现资源未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41889732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com