【问题标题】:How i can assign differents tags to various objects at runtime in Delphi我如何在 Delphi 运行时将不同的标签分配给各种对象
【发布时间】:2020-09-24 04:27:36
【问题描述】:

如何在运行时将不同的标签分配给相同类型的各种对象(例如:TCircle)? 让我解释一下:我想在运行时创建各种圈子,并为每个圈子分配一个不同的标签,然后使用点击事件显示我点击的圈子。 这是我的代码片段:

procedure 
TPhotoX.FormCreate(Sender: 
TObject);
var
 FilesN: String;
 S: TBitmap;
 Cir: TCircle;
begin
  FlowLayout1.DeleteChildren;
  GetFP:= TDirectory.GetFiles(GetPathIma, '*jpg', TSearchOption.soTopDirectoryOnly);

for FilesN in GetFP do
VertScrollBox1.BeginUpdate;
Cir.TCircle.Create(Self);
Cir.Parent:= FlowLayOut1;  
Cir.Fill.Bitmap.WrapMode:=TWrapMode.TileOriginal;
Cir.Fill.Kind:= TBrushkind.Bitmap;
Cir.Height:= 85;
Cir.Width:= 85;

//...more circle's properties next including the Circle's Tag property that i ignore to implement
// Sorry i'm Delphi's Beginner but Delphi's power believer too!!! :-)

Cir.OnClick: CirClick;
try
S.TBitmap.Create;
FlowLayout1.AddObject(Cir);
S.LoadThumbnailsFromFile(FilesN, 150, 150);
Cir.Fill.Bitmap.Bitmap:=S;
Cir.Repaint;
VertScrollBox1.EndUpdate;
finally
S.Free;
end;
end;

//in the code above, how i can to assign differents tags for each circle for referencing later with this handler:

procedure TPhotoX.CirClick(Sender:TObject);
begin
 case TCircle(Sender).Tag of
 1: //event to show the image 
 inside the circle
 2: // event to show another 
image inside the circle
 end;
end;
end; 

感谢任何形式的帮助...谢谢您

【问题讨论】:

  • S.TBitmap.Create; 必须是 S := TBitmap.Create; 并且必须在 before try 行上。
  • Cir.TCircle.Create(Self) 相同,它必须是Cir := TCircle.Create(Self); 并且您已经知道Tag 属性。您还想要什么?
  • 另外,你需要一个 try..finally 块用于 VertScrollBox1.(Begin|End)Update:
  • for 循环不起作用 - 需要一个开始/结束,或者如 Remy 所说,一个 Try/finally
  • 因为您的for 循环是for variable in collection 的形式,您没有自然整数索引来分配每个Cirtag。因此,在procedure TPhotoX.FormCreate() 中声明一个局部变量i: integer,在循环之前为其分配值0,并在为您创建的每个新Cir 分配给Cir.Tag 之前将其递增。

标签: delphi tags runtime firemonkey pascal


【解决方案1】:

正如 cmets 中所指出的,您的代码中有几个错误。您没有正确创建 TBitmapTCircle 对象。您没有充分保护资源。而且您的 for 循环缺少必要的 begin/end 块来包含您的循环逻辑。

而且,为了回答您的问题,由于您使用的是 for..in 循环,如果您想分配基于索引的 Tag 值,那么您需要在迭代时使用单独的变量来跟踪当前索引通过集合。

试试这样的:

procedure TPhotoX.FormCreate(Sender: TObject);
var
  FilesN: String;
  S: TBitmap;
  Cir: TCircle;
  I: Integer;
begin
  FlowLayout1.DeleteChildren;
  GetFP := TDirectory.GetFiles(GetPathIma, '*jpg', TSearchOption.soTopDirectoryOnly);
  if GetFP <> nil then Exit;

  VertScrollBox1.BeginUpdate;
  try
    I := 1;
    for FilesN in GetFP do
    begin
      Cir := TCircle.Create(Self);
      try
        Cir.Parent := FlowLayOut1;  
        Cir.Fill.Bitmap.WrapMode := TWrapMode.TileOriginal;
        Cir.Fill.Kind := TBrushkind.Bitmap;
        Cir.Height := 85;
        Cir.Width := 85;
        Cir.Tag := I; // <-- or whatever you need
        Inc(I);
        Cir.OnClick := CirClick;

        S := TBitmap.Create;
        try
          S.LoadThumbnailsFromFile(FilesN, 150, 150);
          Cir.Fill.Bitmap.Bitmap := S;
        finally
          S.Free;
        end;

        FlowLayout1.AddObject(Cir);
      except
        Cir.Free;
        raise;
      end;

      //Cir.Repaint;
    end;
  finally
    VertScrollBox1.EndUpdate;
  end;
end;

procedure TPhotoX.CirClick(Sender: TObject);
begin
  case TCircle(Sender).Tag of
    1: // event to show the image inside the circle
    2: // event to show another image inside the circle
  end;
end; 

【讨论】:

  • 非常感谢 Remy,我很感谢你的回答,也感谢其他人。我对维护 Object Pascal 语言的知识和遗产表示诚挚的敬意。 ?
猜你喜欢
  • 2018-04-17
  • 2021-01-05
  • 2022-01-24
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
相关资源
最近更新 更多