Class Delphi创建带有图像的按钮
我在创建带有图像和标签的按钮时遇到问题。 这是我的代码: 类别:Class Delphi创建带有图像的按钮,class,delphi,delphi-xe7,Class,Delphi,Delphi Xe7,我在创建带有图像和标签的按钮时遇到问题。 这是我的代码: 类别: type Folder = class(TButton) AName:TLabel; AImage:TImage; constructor Create(Nme:String;Path:String;Handle:TForm); end; 建造商: constructor Folder.Create(Nme:String;Path:String;Handle:TForm); begin A
type
Folder = class(TButton)
AName:TLabel;
AImage:TImage;
constructor Create(Nme:String;Path:String;Handle:TForm);
end;
建造商:
constructor Folder.Create(Nme:String;Path:String;Handle:TForm);
begin
AImage:=Timage.Create(Self);
AName:=TLabel.Create(Self);
AImage.Parent:=Self;
AName.Parent:=Self;
AName.Caption:=Nme;
AImage.Picture.LoadFromFile(Path);
end;`
以及我创建此按钮的事件:
procedure TForm3.Button1Click(Sender: TObject);
var Fld:Folder;
begin
Fld:=Folder.Create('It','D:\image.bmp',Form3);
Fld.Parent:=Form3;
Fld.Width:=100;
Fld.Height:=100;
end;
但当我创建此按钮时,它会导致访问权限冲突!我必须用它做什么?问题:
问题是您已经声明了构造函数的自定义版本,但是您没有调用TButton
类的父构造函数
您需要像这样更改构造函数:
constructor Folder.Create(Nme: String; Path: String; Handle: TForm);
begin
inherited Create(Handle); // <- Add this line
AImage := TImage.Create(Self);
AName := TLabel.Create(Self);
AImage.Parent := Self;
AName.Parent := Self;
AName.Caption := Nme;
AImage.Picture.LoadFromFile(Path);
end;
constructor文件夹.Create(Nme:String;Path:String;Handle:TForm);
开始
继承的创建(句柄);//问题:
问题是您已经声明了构造函数的自定义版本,但是您没有调用TButton
类的父构造函数
您需要像这样更改构造函数:
constructor Folder.Create(Nme: String; Path: String; Handle: TForm);
begin
inherited Create(Handle); // <- Add this line
AImage := TImage.Create(Self);
AName := TLabel.Create(Self);
AImage.Parent := Self;
AName.Parent := Self;
AName.Caption := Nme;
AImage.Picture.LoadFromFile(Path);
end;
constructor文件夹.Create(Nme:String;Path:String;Handle:TForm);
开始
继承的创建(句柄);//我建议您首先阅读有关继承、重写方法、“重新引入”和从自定义控件派生的内容。例如您没有重新引入构造函数,也没有从任何祖先构造函数继承。看看:。暗示;创建从特定祖先派生的组件时,建议保留该祖先的结构并进行相应调整。按钮支持图像alreadyLittle侧注:类名应以T开头,字段名应以“f”开头,而参数应以“a”开头,以避免混淆。另外,构造函数中不使用“Handle”参数。另一个建议:激活项目的“调试信息生成”,以便查看堆栈跟踪。我建议您首先阅读继承、重写方法、“重新引入”和从自定义控件派生。例如您没有重新引入构造函数,也没有从任何祖先构造函数继承。看看:。暗示;创建从特定祖先派生的组件时,建议保留该祖先的结构并进行相应调整。按钮支持图像alreadyLittle侧注:类名应以T开头,字段名应以“f”开头,而参数应以“a”开头,以避免混淆。此外,构造函数中未使用“Handle”参数。另一个建议:激活项目的“调试信息生成”,以便查看堆栈跟踪