Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Coloboración Paypal con ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #5  
Antiguo 07-12-2012
Avatar de ozsWizzard
ozsWizzard ozsWizzard is offline
Miembro
 
Registrado: may 2004
Ubicación: Murcia
Posts: 190
Poder: 23
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
 


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Formato Numerico CyberHacker Conexión con bases de datos 5 02-04-2011 21:15:25
Formato Numerico en Rave servicomp Impresión 1 20-10-2010 00:57:43
como cambiar un formato numerico narcisoplayero SQL 2 05-06-2007 18:43:47
Como dar formato a un campo numerico... chileno Conexión con bases de datos 2 27-04-2006 19:58:22
Formato Numérico javiermorales Varios 5 15-05-2003 12:22:14


La franja horaria es GMT +2. Ahora son las 02:51:56.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi