Ver Mensaje Individual
  #5  
Antiguo 07-12-2012
Avatar de ozsWizzard
ozsWizzard ozsWizzard is offline
Miembro
 
Registrado: may 2004
Ubicación: Murcia
Posts: 190
Reputación: 21
ozsWizzard Va por buen camino
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;

         //Eventos
         property OnExitCell: TExitCell       read FExitCell    write FExitCell; //Este va a ser el evento OnExitCell
         property OnKeyPressCell: TKeyPressCell read FKeyPressCell write FKeyPressCell;
  end;

Implementación
Código Delphi [-]
{ TStringGrid }

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
   //Para no repetir el evento OnExitCell cuando se entra al StringGrid
   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 [-]
//Evento Keypress para los datos float
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;

//Se implementa OnExitCell
procedure Form1.sg1ExitCell(Sender: TObject; const ACol,
  ARow: Integer);  
var
  sg: TStringGrid;
begin
   sg := (Sender as TStringGrid);
   //Esto es para formatear, siempre puedes poner más cosas en el evento si lo ves necesario
   //Sólo se formatean los campos que sabemos que son Float o Currency, en este caso son las columnas 1, 2 y 3 y que no sea la fila 0
   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
   //Se asigna el evento para los campos tipo importe, como en el evento anterior son las columnas 1, 2 y 3 y que no sea la fila 0
   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
__________________
La Madurez se llama...
~~~Gaia~~~
Responder Con Cita