Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Ajustar el width de un TListBox (https://www.clubdelphi.com/foros/showthread.php?t=46728)

salvica 07-08-2007 18:10:05

Ajustar el width de un TListBox
 
Hola a tod@s :)

Tengo un panel que contiene tres ListBox (CLAVE, INICIALES y DESCRIPCION), los cuales cargo desde una Tabla.

El problema consiste en que no soy capaz :( de calcular el tamaño máximo del listbox DESCRIPCIÓN para que se vea TODO el texto que contiene. El código que utilizo es el siguiente:
Código Delphi [-]
{
  ajustar el TPanel contenedor de los TListBox
  ANCHO_TEXTO se declara como integer y global en el Form
}
procedure TForm_GetListas.AjustarContenedor( var Panel:TPanel; ListBox:TListBox );
var
  Panel_CONTENEDOR : TPanel;
begin
{ altura del contenedor }
  Panel_CONTENEDOR        := (ListBox.Parent as TPanel);
  ListBox_TEXTO.Width     := ANCHO_TEXTO;
  Panel_CONTENEDOR.Height := ((ListBox.Items.Count+1)*ListBox.ItemHeight);
  Panel_CONTENEDOR.Width  := ListBox_CLAVE.Width + ListBox_INIC.Width + ListBox_TEXTO.Width;
  Bevel1.Width            := Panel_CONTENEDOR.Left + Panel_CONTENEDOR.Width;
end;


{
  rellenar los TListBox desde la tabla
  ANCHO_TEXTO se declara como integer y global en el Form
}
procedure TForm_GetListas.MYSQL_PasarTipoToListas(  Query:TZQuery;
                                                       strKey:string );
const
  strTabla = 'datos_prueba';
var
  strGrupo, strOrden : string;
  Activo    : integer;
  Ancho     : integer;
  iOrden    : integer;
  Cadena    : string;
begin
  Activo    := ITEM_INICIO;
  Ancho     := 0;
  strGrupo  := Copy( strKey, 1, 3 );
  strOrden  := Copy( strKey, 5, 3 );
  with Query do begin
     { viene preparada de la función que la llama }
       SQL.Clear;
       SQL.Add( 'SELECT * ' );
       SQL.Add( '  FROM ' + strTabla );
       SQL.Add( ' WHERE grupo="' + strGrupo + '"' );
     { abrir la tabla activa }
       Open;
       if IsEmpty then begin
          //Memo.Text := 'MYSQL - No encontrada la clave ['+strKey+']';
       end else begin
          First;
          while not eof do begin
                iOrden   := StrToInt( FieldByName('ORDEN').AsString ); // div 10;
                Cadena   := FieldByName('TEXTO' ).AsString;
              { 
                ¿aquí compruebo el acho que debe tener?
              }
                Ancho    := ListBox_TEXTO.Canvas.TextWidth( Cadena );
                if( Ancho>ANCHO_TEXTO ) then ANCHO_TEXTO := Ancho;
              { 
                rellenar los TListBox
              }
                ListBox_CLAVE.Items.Add( FieldByName('CLAVE').AsString );
                ListBox_INIC.Items.Add( FieldByName('INICIALES').AsString );
                ListBox_TEXTO.Items.Add( Trim(Cadena) );
              { 
                si es la clave, activar el TListBox
              }
                if( FieldByName('CLAVE').AsString=strKey )
                    then Activo := ListBox_ORDEN.Items.Count-1;
                Next;
          end;
       end;
     { cerrar la tabla activa }
       Close;
  end; { del with Query do }
{ dar tamaño al contenedor de Listas }
  AjustarContenedor( Panel_TABLA, ListBox_CLAVE );
  Panel_DESCRIBE_0.Caption := IntToStr( Ancho );
{ activar las listas }
  ActivarTablas( Activo );
end;

Gracias de antemano
Salvica

ixMike 08-08-2007 12:36:06

Hola.

Para saber el ancho de un ListBox yo utilizo esto:

Código Delphi [-]
Procedure TForm1.Button1Click(Sender: TObject);
var ancho, n: integer;
begin
ancho:=0;
for n:=0 to ListBox1.Items.Count-1 do
 if ListBox1.Canvas.TextWidth(ListBox1.Items[n])>ancho then
  ancho:=ListBox1.Canvas.TextWidth(ListBox1.Items[n]);
ListBox1.Width:=Ancho;
end;

Espero que te sea de ayuda.

Saludos :)

salvica 11-08-2007 20:04:01

Cita:

Empezado por ixMike (Mensaje 221539)
Hola.

Para saber el ancho de un ListBox yo utilizo esto:

Código Delphi [-]Procedure TForm1.Button1Click(Sender: TObject);
var ancho, n: integer;
begin ancho:=0; for n:=0 to ListBox1.Items.Count-1 do if ListBox1.Canvas.TextWidth(ListBox1.Items[n])>ancho then ancho:=ListBox1.Canvas.TextWidth(ListBox1.Items[n]); ListBox1.Width:=Ancho; end;


Espero que te sea de ayuda.

Saludos :)

Me ha servido de muuuuuuucha ayuda ;)
Gracias
Salvica


La franja horaria es GMT +2. Ahora son las 13:08:20.

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