procedure TFDBForm_EditForm_MD.EnableDataSetEvent(AEnalbed: Boolean);
{$J+}
const
CDetailBeforeEditEvent: TDataSetNotifyEvent = nil;
CDetailBeforeDeleteEvent: TDataSetNotifyEvent = nil;
{$J-}
begin
inherited;
if AEnalbed then
begin
if not Assigned(CDetailBeforeEditEvent) then CDetailBeforeEditEvent := OnDetailDataSetBeforeEdit;//
if not Assigned(CDetailBeforeDeleteEvent) then CDetailBeforeDeleteEvent := OnDetailDataSetBeforeDelete;//
DetailDataSet.BeforeEdit := CDetailBeforeEditEvent;
DetailDataSet.BeforeDelete := CDetailBeforeDeleteEvent;
DetailDataSet.BeforeInsert := OnDetailDataSetBeforeInsert;
end else
begin
DetailDataSet.BeforeEdit := nil;
DetailDataSet.BeforeDelete := nil;
DetailDataSet.BeforeInsert := nil;
end;
end;
上述代码似乎没有什么问题。
但是在窗体关闭再打开之后就出现问题了
if not Assigned(CDetailBeforeEditEvent) then CDetailBeforeEditEvent := OnDetailDataSetBeforeEdit;//
if not Assigned(CDetailBeforeDeleteEvent) then CDetailBeforeDeleteEvent := OnDetailDataSetBeforeDelete;//
注意这两句,在第一次被赋值以后,即使窗体关闭(Free),再次打开之后,因为CDetailBeforeEditEvent 是常量,还记录了前次的地址,但前次的地址中所指的事件已经销毁了,所以会引发错误。
{$J+}
const
CDetailBeforeEditEvent: TDataSetNotifyEvent = nil;
CDetailBeforeDeleteEvent: TDataSetNotifyEvent = nil;
{$J-}
begin
inherited;
if AEnalbed then
begin
if not Assigned(CDetailBeforeEditEvent) then CDetailBeforeEditEvent := OnDetailDataSetBeforeEdit;//
if not Assigned(CDetailBeforeDeleteEvent) then CDetailBeforeDeleteEvent := OnDetailDataSetBeforeDelete;//
DetailDataSet.BeforeEdit := CDetailBeforeEditEvent;
DetailDataSet.BeforeDelete := CDetailBeforeDeleteEvent;
DetailDataSet.BeforeInsert := OnDetailDataSetBeforeInsert;
end else
begin
DetailDataSet.BeforeEdit := nil;
DetailDataSet.BeforeDelete := nil;
DetailDataSet.BeforeInsert := nil;
end;
end;
上述代码似乎没有什么问题。
但是在窗体关闭再打开之后就出现问题了
if not Assigned(CDetailBeforeEditEvent) then CDetailBeforeEditEvent := OnDetailDataSetBeforeEdit;//
if not Assigned(CDetailBeforeDeleteEvent) then CDetailBeforeDeleteEvent := OnDetailDataSetBeforeDelete;//
注意这两句,在第一次被赋值以后,即使窗体关闭(Free),再次打开之后,因为CDetailBeforeEditEvent 是常量,还记录了前次的地址,但前次的地址中所指的事件已经销毁了,所以会引发错误。