Hola
Procedimiento calcula:
Colocar el procedure al principio, asi:
Código Delphi
[-]
procedure DBEdit5Change(Sender: TObject);
procedure calcula; private
public
end;
var
Form14: TForm14;
fecha:Tdate;
S,D,I,total:Real;
e,c:integer;
implementation
Crear el procedimiento:
Código Delphi
[-]
procedure calcula;
var
TotalGanancias : Currency;
Marca : TBookMarkStr;
begin
TotalGanancias := 0;
Marca := Table2.Bookmark; Table2.DisableControls; Table2.First;
While Not Table2.Eof do
begin
TotalGanancias := TotalGanancias + Table2.FieldbyName('TotalImp').ASCurrency;
Table2.Next;
end;
Table2.EnableControls; Table2.Bookmark := Marca; Edit2.Text:=CurrToStr(TotalGanancias);
S:=StrToFloat(Edit2.Text);
total:=S-D+I;
Edit5.Text:=FloatToStr(total);
end;
Colocar el procedimiento en el dobleclick del dbgrid2: (revisa al final del codigo es lo unico que cambia)
Código Delphi
[-]
procedure TForm14.DBGrid2DblClick(Sender: TObject);
begin
Table2.Open;
if combobox1.Text = 'Productos' then
begin
datasource5.DataSet:= Table4; Table2.Insert; Table2Cod_prodser.Value:= Table4Cod_prod.Value; Table2Codigo.Value:= Table4Codigo.Value;
Table2Descripcion.Value:= Table4Descripcion.Value;
Table2Importunidad.Value:= Table4ImpVent.Value;
Table2Cantidad.Value:= StrToFloat(Edit1.Text); Table2TotalImp.Value:=Table2Cantidad.Value * Table2Importunidad.Value; Table2.Post; end;
if combobox1.Text = 'Servicios' then
begin
datasource3.DataSet:= Table3; Table2.Insert; Table2Cod_prodser.Value:= Table5Cod_servcio.Value; Table2Codigo.Value:= Table5Codigo.Value;
Table2Descripcion.Value:= Table5Servicio.Value;
Table2Cantidad.Value:= StrToFloat(Edit1.Text); Table2Importunidad.Value:= Table5ImpServ.Value;
Table2TotalImp.Value:=Table2Cantidad.Value * Table2Importunidad.Value;
Table2.Post; end;
calcula; end;
Llamar al procedimiento en el edit3 y el 4 en el evento onchange:
Código Delphi
[-]
procedure TForm14.Edit3Change(Sender: TObject);
begin
D:=StrToFloat(Edit2.text)*StrToFloat(Edit3.Text)/100;
if edit2.Text = ' ' then
begin
edit2.Text = '0'.
end
else
calcula;
end;
Código Delphi
[-]
procedure TForm14.Edit4Change(Sender: TObject);
begin
I:=StrToFloat(Edit2.text)*StrToFloat(Edit4.Text)/100;
if edit4.Text = ' ' then
begin
edit4.Text = '0'.
end
else
calcula;
end;
Parece mucho trabajo ya que tienes que modificar algunas cosas, pero te daras cuenta que crear procedimientos ajustados a tu necesidad te ayuda a no repetir codigo, simplemente lo llamas cuando necesites.
saludos
Espero que estemos aprendiendo algo a nuestra edad....

