Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   alinear a la derecha texto de un stringgrid (https://www.clubdelphi.com/foros/showthread.php?t=14469)

ctronx 21-09-2004 19:19:33

alinear a la derecha texto de un stringgrid
 
Saludos, como podria alinear a la derecha el contenido de una columna de un stringgrid, tomando en cuenta que mi aplicacion es CLX.

Gracias por la ayuda que puedan darme.

yusnerqui 22-09-2004 14:22:38

Primero que nada quiero pedirte disculpa por mi falta de conocimiento (No se que es una aplicación CLX:confused: :confused: :confused: ), pero quizás el truco 234 de Trucomanía te pueda ayudar, a continuación te lo cito.

Cita:

Empezado por Trucomanía
Pon este código en el evento OnDrawCell de tu StringGrid:

Código Delphi [-]
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
var sCad:String;
    i:integer;
    bEsNum:Bool;
begin 
  if StringGrid1.Cells[Col,Row] <> '' then 
  begin 
     bEsNum := True;
     sCad := StringGrid1.Cells[Col,Row];
     for i:=1 to length(sCad) do 
         if not (sCad[i] in ['0'..'9','-']) then bEsNum := False;
     if bEsNum then 
     begin 
        {'%15 para una longitud maxima de 15 caracteres}
        {'%15 for a Max Length of 15 characteres}
        sCad := Format('%15.0n',[StrToFloat(StringGrid1.Cells[Col,Row])]);
        with StringGrid1 do 
          with Canvas,Rect do 
          begin 
             i:=Right-TextWidth(sCad+' ');
             Canvas.FillRect(Rect);
             Canvas.TextOut(i,Top+2,sCad);
          end; 
     end; 
  end; 
end;

Espero que eso te pueda ayudar.:)

Un saludo


ctronx 22-09-2004 21:48:36

Saludos, gracias por tu ayuda pero tu ejemplo no me permite especificar una determinada columna para alinear a la derecha.

este ejemplo si me pide la columna que quiero alinear pero me marca que no esta declarada ó definida DrawText, a de faltar una Unit ó algo asi, espero me puedan ayudar.

Código Delphi [-]

procedure Tfrmventas.StringGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
  begin
if ACol=2 then // voy a alinear a la derecha la 3ª columna
if ARow>0 then //No quiero alinear la línea de títulos
with StringGrid1 do
     begin
     Canvas.FillRect(Rect);
     DrawText(Canvas.Handle,PChar(Cells[Acol,ARow]),-1,Rect,
          DT_CENTER or DT_RIGHT or DT_SINGLELINE);
     end;

end;

roman 22-09-2004 22:14:46

Cita:

Empezado por ctronx
pero tu ejemplo no me permite especificar una determinada columna para alinear a la derecha.

¿Cómo está eso de que no te permite elegir la columna? ¡Se trata del mismo evento! Así que en ambos casos puedes usar condicionales para ACol y ARow.

// Saludos

yusnerqui 22-09-2004 23:04:50

Esto Funciona
 
Como bien dice el amigo roman, se trata del mismo evento así que con solo unas pequeñas modificaciones lo podrás adaptar a lo que quieres.
Código Delphi [-]
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var sCad:String;
    i: integer;
begin 
  if ACol=2 then // voy a alinear a la derecha la 3ª columna
  if ARow>0 then //No quiero alinear la línea de títulos
    if StringGrid1.Cells[ACol,ARow] <> '' then 
    begin 
       sCad := StringGrid1.Cells[ACol,ARow];
         with StringGrid1 do 
            with Canvas,Rect do 
            begin 
               i:=Right-TextWidth(sCad+' ');
               Canvas.FillRect(Rect);
               Canvas.TextOut(i,Top+2,sCad);
            end;      
    end; 
end;


Esto lo probé y me funciona.:)

Espero te Ayude.

Un saludo:D

ctronx 23-09-2004 20:27:50

Ok, muchas gracias a ambos.

BlueSteel 06-06-2006 16:30:04

problema al alinear
 
Hola amigos...:D

tengo un pequeñisimo problema... quise ejecutar esta sentencia.. pero me da el siguente error

[Error] Unit5.pas(399): Undeclared identifier: 'Right'

he tratado de solucionarlo.,... pero no se como.. el codigo es tengo es el sgte:

Código Delphi [-]
procedure TOrden.Items_PasoDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var  sCad : String;
    i : Integer;
begin
     If (ACol=2) Or (ACol=3) Then
        If Items_Paso.Cells[ACol,ARow] <> '' Then
           Begin
                sCad := Items_Paso.Cells[ACol,ARow];
                With Items_Paso Do
                Begin
                    i:=Right-TextWidth(sCad+' ');
                     Canvas.FillRect(Rect);
                     Canvas.TextOut(i,Top+2,sCad);
                End;
           End;
end;

espero me puedan ayudar..

BlueSteel 06-06-2006 16:32:08

ya lo arregle.. me habia comido una linea.. sorry..y gracias:p

FGarcia 07-09-2007 21:02:32

Hola!

Resucitando este hilo ya que necesito hacer algo similar ¿cual fue la linea que te comiste Blue Steel?

BlueSteel 07-09-2007 21:40:02

Cita:

Empezado por FGarcia (Mensaje 229248)
Hola!

Resucitando este hilo ya que necesito hacer algo similar ¿cual fue la linea que te comiste Blue Steel?


Hola.. en realidad ya ni me acuerdo.. por que fue hace mas de 1 año... pero si te sale error.. por que mejor no pones tu codigo...

FGarcia 07-09-2007 21:54:59

JEJE!

Como me fui a comer no habia probado nada del codigo pero copy & paste el tuyo, le cambie el nombre del stringgrid y me aparece el mismo error que mencionas

edito: OK Corregido

Código Delphi [-]
procedure TfrmMain.StringGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var sCad: String;
    i: Integer;
begin
  If (ACol=1) Or (ACol=2) Then
    If StringGrid1.Cells[ACol,ARow] <> '' Then
      Begin
        sCad := StringGrid1.Cells[ACol,ARow];
        With StringGrid1 Do
          with Canvas,Rect do   //ESTA es la linea que faltaba
          Begin
            i:=Right-TextWidth(sCad+' ');
            Canvas.FillRect(Rect);
            Canvas.TextOut(i,Top+2,sCad);
          End;
      End;

end;


La franja horaria es GMT +2. Ahora son las 03:11:59.

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