Yo me creé el evento "OnExitCell" para tratar importes también, a la vez me creé el evento "OnKeyPressCell" no sólo para formatear los importes al salir, sino para que sólo deje meter números y el separador decimal. Aparte, con el "Enter" simula el pulsado del "F2" pero eso se puede quitar.
El evento "OnKeyPressCell", realmente no hace falta pero es por si quieres separarlo del OnKeyPress normal.
Definición
Código Delphi
[-]
TExitCell = procedure(Sender: TObject; const ACol, ARow: Integer) of object;
TKeyPressCell = procedure(Sender: TObject; const ACol, ARow: Integer; var Key: Char) of object;
TStringGrid = class(Vcl.Grids.TStringGrid)
private
FCol: Integer;
FRow: Integer;
FEntrar: Boolean;
EditCel: Boolean;
FExitCell: TExitCell;
FKeyPressCell: TKeyPressCell;
protected
function SelectCell(ACol, ARow: LongInt): Boolean; override;
procedure DoEnter; override;
procedure DoExit; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
public
property ColAux: Integer read FCol write FCol default 0;
property RowAux: Integer read FRow write FRow default 0;
property Entrar: Boolean read FEntrar write FEntrar;
property OnExitCell: TExitCell read FExitCell write FExitCell; property OnKeyPressCell: TKeyPressCell read FKeyPressCell write FKeyPressCell;
end;
Implementación
Código Delphi
[-]
procedure TStringGrid.DoEnter;
begin
inherited;
ColAux := Col;
RowAux := Row;
Entrar := true;
end;
procedure TStringGrid.DoExit;
begin
if Assigned(OnExitCell) then OnExitCell(Self, ColAux, RowAux);
inherited;
end;
procedure TStringGrid.KeyDown(var Key: Word; Shift: TShiftState);
begin
if Key = VK_RETURN then
begin
if not EditCel then
begin
keybd_event(VK_F2, 0, 0, 0);
keybd_event(VK_F2, 0, KEYEVENTF_KEYUP, 0);
Key := 0;
end;
EditCel := not EditCel;
end;
inherited;
end;
procedure TStringGrid.KeyPress(var key: Char);
begin
if Assigned(OnKeyPressCell) then OnKeyPressCell(Self, ColAux RowAux, Key);
inherited;
end;
function TStringGrid.SelectCell(ACol, ARow: LongInt): Boolean;
begin
if Entrar then
Entrar := false
else
if Assigned(OnExitCell) then OnExitCell(Self, ColAux, RowAux);
inherited;
ColAux := ACol;
RowAux := ARow;
EditCel := false;
Result := true;
end;
Programación y asignación de eventos para Float (más bien Currency)
sq1 sería el TStringGrid
Código Delphi
[-]
procedure sgFloatKeyPress(Sender: TObject; const ARow: Integer;
var Key: Char);
begin
if not (Key in [#9, #13]) then
begin
if (key = ',') or (key='.') then
key := DecimalSeparator;
if not ( ((key >= '0') and (key <= '9')) or (key=#8) or (key=DecimalSeparator) ) then
key := #0
else if (key = DecimalSeparator ) and (AnsiPos(DecimalSeparator,text)<>0) then
key := #0;
end;
end;
procedure Form1.sg1ExitCell(Sender: TObject; const ACol,
ARow: Integer);
var
sg: TStringGrid;
begin
sg := (Sender as TStringGrid);
if (ARow > 0) and (ACol in [1, 2, 3]) then
if (sg.Cells[ACol, ARow] <> EmptyStr) then
sg.Cells[ACol, ARow] := FormatFloat('#,#0.#0', StrToCurr(sg.Cells[ACol, ARow]));
end;
procedure Form1.sg1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
begin
if (ARow > 0) and (ACol in [1, 2, 3]) then
TStringGrid(Sender).OnKeyPressCell := sgFloatKeyPress
else
TStringGrid(Sender).OnKeyPressCell := nil;
end;
procedure Form1.FormCreate(Sender: TObject);
begin
sg1.OnExitCell := sg1ExitCell;
end;
Espero que te sirva