gpt4 book ai didi

delphi - 将函数添加到事件运行时

转载 作者:行者123 更新时间:2023-12-02 02:18:57 25 4
gpt4 key购买 nike

procedure TFormOrderAdd.DBEdit1DblClick(Sender: TObject);
var
FormSelectEmp: TForm;
SelectEmpDBGrid: TDBGrid;

begin
FormSelectEmp := TForm.Create(Self);
SelectEmpDBGrid := TDBGrid.Create(Self);
SelectEmpDBGrid.Parent := FormSelectEmp;
SelectEmpDBGrid.Align := alClient;
SelectEmpDBGrid.DataSource := DMl.DataSourceViewEmpList;
FormSelectEmp.ShowModal;
SelectEmpDBGrid.OnDblClick := AddSelectedEmp;
FormSelectEmp.Close;
end;

procedure TFormOrderAdd.AddSelectedEmp;
begin
DBEdit1.Text := Dml.ADOQueryViewEmpList.FieldByName('ID').Text;
end;

如何将我的过程添加到 OnDblClick 事件?我尝试只是分配,但编译器说: [dcc32 Error] OrderAdd.pas(66): E2010 不兼容的类型:“TNotifyEvent”和“过程、非类型化指针或非类型化参数”

最佳答案

您必须在类中创建一个匹配的事件方法原型(prototype),然后将其分配给事件处理程序(对于 TDBGrid 控件的 OnDblClick 事件,它是 TNotifyEvent ),因此您可以编写以下示例:

type
TForm1 = class(TForm)
DBEdit1: TDBEdit;
DBGrid1: TDBGrid;
private
procedure Form1Create(Sender: TObject);
procedure MyGridDblClick(Sender: TObject);
end;

implementation

procedure TForm1.Form1Create(Sender: TObject);
begin
{ it doesn't matter if you create the component at runtime,
this is a common principle of assigning event methods at
runtime - they just have to match the method prototypes }
DBGrid1.OnDblClick := MyGridDblClick;
end;

procedure TForm1.MyGridDblClick(Sender: TObject);
begin
{ to access the grid instance in case more than one grid
uses this handler you can use TDBGrid(Sender) or safer
cast (Sender as TDBGrid) }
DBEdit1.Text := TDBGrid(Sender).DataSource.Dataset.FieldByName('ID').Text;
end;

关于delphi - 将函数添加到事件运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44357513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com