1

我正在设置一个新程序,它将在执行查询后显示一条消息。我正在使用“AfterOpen”事件,我必须传递“DataSet:TDataSet”参数。

procedure Tf_SeznamDluzniku.ShowInfoMessage(DataSet: TDataSet; info : string);
begin
  l_InfoMessage.Caption := info;
  img_success.Visible := True;
end;
query.AfterOpen := ShowInfoMessage(,'InfoMessage here')

有人可以解释一下 DataSet 变量是什么以及我必须将什么作为第一个参数传递给程序吗?

4

1 回答 1

1

如果它附加到事件,则它是触发 AfterOpen 事件的数据集。数据集本身将调用该过程,并在该参数中传递自身。

但是您添加了 Info 参数,这使得该过程作为事件处理程序无效。您希望这些信息来自哪里?来自数据集?

由于它是一个事件处理程序,因此自己调用它是一种不好的做法。您可以这样做,然后通过nil(或特定数据集),因为它无论如何都没有使用。但是您可能会遇到奇怪的情况,因为看起来该方法只会在打开后被调用,但事实证明它也在其他场合被调用。所以最好创建一个单独的过程来做你想做的事情,并从 AfterOpen 事件处理程序中调用它。您可以从数据集中传递信息,但您也可以从其他地方调用该过程,例如在打开数据集之前提供一些初始标题:

// The procedure doesn't need the dataset, only the info to set.
procedure Tf_SeznamDluzniku.ShowInfoMessage(Info : string);
begin
  l_InfoMessage.Caption := info;
end;

// The actual event handler for YourDataset.OnAfterOpen (you have to attach them)
// This can set the info, and/or maybe set the success indicator right away..
procedure Tf_SeznamDluzniku.YourDataSetAfterOpen(DataSet: TDataSet);
begin
  ShowInfoMessage(DataSet.FieldByName('info').AsString);
  img_success.Visible := True;
end;

// For demonstration, another event handler for showing the form, to put in some initial caption.
procedure Tf_SeznamDluzniku.FormShow(const Sender: TObject);
begin
  ShowInfoMessage('Loading...');
end;
于 2019-07-24T12:33:23.570 回答