我在我的代码中遇到问题,但找不到解决方案,已经以各种方式修改但没有成功.
码:
private
{ Private declarations }
procedure getImgInfo(Sender: TObject; A, B: String);
Run Code Online (Sandbox Code Playgroud)
和:
procedure TfMain.Button1Click(Sender: TObject);
var
i, Idx, Left, Top, Count : integer;
Graph : TGraphic;
Img : TImage;
EdPath, EdFileName : TEdit;
begin
openImg.Execute;
Left := 5;
Top := 5;
Count := 0;
Idx := 0;
for i:=0 to openImg.Files.Count-1 do
begin
try
begin
Graph := TPngImage.Create;
Graph.LoadFromFile(openImg.Files[i]);
EdPath := TEdit.Create(pImgs);
EdPath.Left := Left + 101;
EdPath.Visible := False;
EdPath.Text := ExtractFilePath(openImg.Files[i]);
EdFileName := TEdit.Create(pImgs);
EdFileName.Left := Left + 101;
EdFileName.Visible := False;
EdFileName.Text := ExtractFileName(openImg.Files[i]);
Img := TImage.Create(pImgs);
Img.Parent := pImgs;
Idx := Idx + 1;
Img.Name := 'Img_'+IntToStr(Idx);
Img.Width := 100;
Img.Height := 100;
Img.Left := Left;
Img.Proportional := True;
Left := Left + 101;
Img.Top := Top;
Img.Picture.Assign(Graph);
Img.BringToFront;
Count := Count + 1;
Img.OnClick := getImgInfo(Img, edPath.Text, edFileName.Text); //Error line
if Count = 2 then
begin
Left := 5;
Top := Top + 101;
Count := 0;
end;
end;
except on E : Exception do
ShowMessage('Error: :' + E.Message);
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
错误:
[dcc32错误] uMain.pas(74):E2010不兼容类型:'TNotifyEvent'和'过程,无类型指针或无类型参数'
怎么了?谢谢!
该OnClick事件处理TImage是一个TNotifyEvent,这样你就可以只分配这样一个过程吧.这是一种方法(属于对象的过程),它采用Sender类型的单个参数TObject.所以这将有效:
procedure TfMain.ImageClickHandler(Sender: TObject);
begin
// Do something
end;
Run Code Online (Sandbox Code Playgroud)
...
Img.OnClick := ImageClickHandler;
Run Code Online (Sandbox Code Playgroud)
您需要某种数据结构来存储数据.也许
type
TImageData = record
Image: TImage;
ImageTitle: string;
ImageFileName: string;
Photographer: string;
DateTaken: TDateTime;
end;
Run Code Online (Sandbox Code Playgroud)
和
var
ImageData = array of TImageData;
Run Code Online (Sandbox Code Playgroud)
或者,更类似于您的代码:
type
TImageData = record
Image: TImage;
AssociatedEditControl1,
AssociatedEditControl2: TEdit;
end;
var
ImageData = array of TImageData;
Run Code Online (Sandbox Code Playgroud)
然后设置ImageDatato 的长度openImg.Files.Count,并使用Image和AssociatedEditControl1而AssociatedEditControl2不是局部变量.毕竟,您希望能够轻松访问这些控件.您还可以设置Tag的TImage到的电流值i,然后ImageClickHandler,你可以检查Self.Tag访问ImageData[Self.Tag].AssociatedEditControl1,说.
(但我仍然认为你应该更好地将内部数据与GUI分开.你还需要修复你的内存泄漏.)
| 归档时间: |
|
| 查看次数: |
1180 次 |
| 最近记录: |