Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   ListBox con dos lineas por item (https://www.clubdelphi.com/foros/showthread.php?t=88166)

elrayo76 21-04-2015 16:54:49

ListBox con dos lineas por item
 
Quisiera saber si existe la posibilidad de poner dos o mas lineas por cada item dentro de un ListBox. De no poderse tendre que reemplazar el control o tro que si me deje como podría ser una grilla.

Quería saber esto para no tener que cambiar el código existente en lo posible.

Saludos,
EL Rayo

ecfisa 21-04-2015 18:46:02

Hola elrayo76.

Si, es posible.
Código Delphi [-]
procedure TForm1.FormCreate(Sender: TObject);
begin
  //...
  Listbox1.Style := lbOwnerDrawVariable;  
end;

procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
var
  cv: TCanvas;
  H : HWND;
  R : TRect;
begin
  cv := TCanvas.Create;
  try
    cv.Handle := GetDeviceContext(H);
    R  := ListBox1.ClientRect;
    Height := DrawText(cv.Handle,PChar(ListBox1.Items[Index]), -1, R, DT_WORDBREAK);
  finally
    cv.Free;
  end;
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  cv: TCanvas;
begin
  cv:= Listbox1.Canvas;
  cv.FillRect(Rect);
  DrawText(cv.Handle, PChar(ListBox1.Items[Index]), -1, Rect, DT_LEFT+DT_TOP+DT_WORDBREAK);
  Invalidate;
end;

Salida:


Saludos :)

nlsgarcia 21-04-2015 20:25:04

elrayo76,

Cita:

Empezado por elrayo76
...Quisiera saber si existe la posibilidad de poner dos o mas lineas por cada item dentro de un ListBox...

:rolleyes:

Una alternativa a N líneas por item en un TListBox, es asociar la data que se requiera a cada item dentro del mismo componente.

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  SL : TStringList;
  L : TList;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
   L := TList.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   i,j : Integer;

begin

   Randomize;

   for i := 1 to 5 do
   begin
      SL := TStringList.Create;
      for j := 1 to 5 do
         SL.Add('Item-' + IntToStr(i) + ' Data-' + IntToStr(Random(100)));

      L.Add(SL);

      ListBox1.AddItem('Item-' + IntToStr(i),SL);
   end;

end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
   AuxSL : TStringList;
   i : Integer;

begin

   AuxSL := TStringList.Create;
   AuxSL.Assign(TStringList(ListBox1.Items.Objects[ListBox1.ItemIndex]));

   Memo1.Clear;
   for i := 0 to AuxSL.Count - 1 do
      Memo1.Lines.Add(AuxSL.Strings[i]);

   AuxSL.Free;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
   i : Integer;

begin

   Action := caFree;

   for i := 0 to L.Count - 1 do
      Dispose(L.Items[i]);

   L.Free;

end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32, Permite asociar un TStringList a cada Item de un TListbox, como se muestra en la siguiente imagen:



Espero sea útil :)

Nelson.

elrayo76 21-04-2015 20:25:32

Muchas gracias amigo

elrayo76 21-04-2015 23:30:15

nlsgarcia, gracias por responder, pero tengo que aclarar que la opción que me dio el amigo ecfisa es lo que realmente buscaba.

Saludos,
El Rayo


La franja horaria es GMT +2. Ahora son las 03:50:25.

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