有没有办法从 Dynamic Object like 和 ID 发送数据?

Is there a way to send data from a Dynamic Object like and ID?

所以我必须创建一个很像 Instagram 或 9Gag 的软件,如果按下 Upvote 按钮,我想使用 Post_ID 和 User_ID 添加到 Upvote table 在我的数据库中。由于正在动态创建的面板是我想使用该数据的按钮的 parents,但 OnClick 事件只能接受 1 个参数。谁能帮忙

为了纠正自己,我使用图像而不是按钮,通常我会使用按钮的标题来存储数据,但我不能使用 TImage。


procedure dynImgUpvoteClick(Sender: TObject);
begin
showmessage('Clicked');
end;

dynImgUpvote.OnClick := dynImgUpvoteClick;

我想这样写,以便在单击按钮时我可以发送 Post_ID 和 User_ID 与 SQL 一起使用以添加到数据库中。但是我不能使用多个参数。

我有以下建议:

  1. 使用 Tag 属性 是一种错误的形式,通常不建议使用。

  2. 您可以创建包含任意数量属性的后代 class: 例如:

type TMyImage = class(TImage)
      strict private
        FStrParam: string;
        FIntParam: Integer;
      public
        property StrParam: string read FStrParam write FStrParam;
        property IntParam: string read FIntParam write FIntParam;
      end;

然后您动态创建图像并设置所有必要的属性:

procedure TForm1.Button1Click(Sender: TObject);
var
  Img: TMyImage;
begin
  Img := TMyImage.Create(Self);
  {Set other properties: size, positipn etc.}
  Img.StrParam := 'Some string';
  Img.IntParam := 123;
  Img.OnClick := dynImgUpvoteClick;
end;

并处理您的类型 OnClick 处理程序中的任何属性:

procedure dynImgUpvoteClick(Sender: TObject);
begin
  showmessage('Clicked ' + (Sender as TMyImage).StrParam +
    IntToStr((Sender as TMyImage).IntParam));
end;