不要在UI元素的事件中計算字段,你能夠在單價字段和數量字段的OnChange事件中進行計算。假設數據集名稱爲DataSet,在數據模塊中編寫一個過程:
procedure CalcMoney(Sender: TField);
begin
if (DataSet.State in dsEditModes) and (not DataSet.FieldByName('Qty').IsNull) and (not DataSet.FieldByName('Price').IsNull)
then
DataSet.FieldByName('Money').AsCurrency := DataSet.FieldByName('Qty').AsFloat * DataSet.FieldByName('Price').AsCurrency;
end;而後在數據集的AfterOpen和BeforeClose事件中添加以下代碼:
AfterOpen:
DataSet.FieldByName('Qty').OnChange := CalcMoney;
DataSet.FieldByName('Price').OnChange := CalcMoney;
BeforeClose;
DataSet.FieldByName('Qty').OnChange := Nil;
DataSet.FieldByName('Price').OnChange := Nil;
procedure CalcMoney(Sender: TField);
begin
if (Sender.DataSet.State in dsEditModes) and (not Sender.DataSet.FieldByName('Counts').IsNull)
and (not Sender.DataSet.FieldByName('Price').IsNull)
then
Sender.DataSet.FieldByName('Money').AsCurrency := Sender.DataSet.FieldByName('Counts').AsFloat * Sender.DataSet.FieldByName('Price').AsCurrency;
end;改爲這個以後,編譯時提示:
E2009 Incompatible types:'method pointer and regular procedure'
procedure TForm1.CalcMoney(Sender: TField);
begin
if (Sender.DataSet.FieldByName('Counts').IsNull) then
Sender.DataSet.FieldByName('Counts').AsFloat := 0;
if (Sender.DataSet.FieldByName('Price').IsNull) then
Sender.DataSet.FieldByName('Price').AsCurrency := 0;
if (Sender.DataSet.State in dsEditModes) and (not Sender.DataSet.FieldByName('Counts').IsNull)
and (not Sender.DataSet.FieldByName('Price').IsNull)
then
Sender.DataSet.FieldByName('Money').AsCurrency := Sender.DataSet.FieldByName('Counts').AsFloat * Sender.DataSet.FieldByName('Price').AsCurrency;
end;我試了,這樣能夠實現。
procedure CalcMoney(Sender: TField);
begin
if (DataSet.State in dsEditModes) and (not DataSet.FieldByName('Qty').IsNull) and (not DataSet.FieldByName('Price').IsNull)
then
DataSet.FieldByName('Money').AsCurrency := DataSet.FieldByName('Qty').AsFloat * DataSet.FieldByName('Price').AsCurrency;
end;而後在數據集的AfterOpen和BeforeClose事件中添加以下代碼:
AfterOpen:
DataSet.FieldByName('Qty').OnChange := CalcMoney;
DataSet.FieldByName('Price').OnChange := CalcMoney;
BeforeClose;
DataSet.FieldByName('Qty').OnChange := Nil;
DataSet.FieldByName('Price').OnChange := Nil;
begin
if (Sender.DataSet.State in dsEditModes) and (not Sender.DataSet.FieldByName('Counts').IsNull)
and (not Sender.DataSet.FieldByName('Price').IsNull)
then
Sender.DataSet.FieldByName('Money').AsCurrency := Sender.DataSet.FieldByName('Counts').AsFloat * Sender.DataSet.FieldByName('Price').AsCurrency;
end;改爲這個以後,編譯時提示:
E2009 Incompatible types:'method pointer and regular procedure'
begin
if (Sender.DataSet.FieldByName('Counts').IsNull) then
Sender.DataSet.FieldByName('Counts').AsFloat := 0;
if (Sender.DataSet.FieldByName('Price').IsNull) then
Sender.DataSet.FieldByName('Price').AsCurrency := 0;
if (Sender.DataSet.State in dsEditModes) and (not Sender.DataSet.FieldByName('Counts').IsNull)
and (not Sender.DataSet.FieldByName('Price').IsNull)
then
Sender.DataSet.FieldByName('Money').AsCurrency := Sender.DataSet.FieldByName('Counts').AsFloat * Sender.DataSet.FieldByName('Price').AsCurrency;
end;我試了,這樣能夠實現。