Multiple lines in one row of a listview?

Can someone tell me how I can add Multiple lines in one row of a listview?
LVL 2
wqclatreAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

steve_hskCommented:
Hi wq ...

I'm assuming the listview.viewstyle := vsreport ?

Also we must set this parameter, to tell the delphi we want to draw it ourselves :-
listview.OwnerDraw := True;

you basically have to redraw all the items very time the onDraw event is called.

The following code is just a listview on a form, with the onDrawItem event of the listview linked to the handler and the Form's onCreate also linked to the handler shown below :

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure ListView1DrawItem(Sender: TCustomListView; Item: TListItem; Rect: TRect; State: TOwnerDrawState);
    procedure FormCreate(Sender: TObject);
  public
    ta_Bottom : Array [1 .. 5] of Integer;
    ta_Color  : Array [0 .. 4] of TColor;
end;

var Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
    ta_Color[0] := clLime;
    ta_Color[1] := clBlue;
    ta_Color[2] := clYellow;
    ta_Color[3] := clAqua;

    ListView1.OwnerDraw := TRUE;
    ListView1.ViewStyle := vsReport;

    ListView1.Columns.Add;
    ListView1.Columns.Items[0].Caption := 'COL TITLE';
    ListView1.Columns.Items[0].Width := 80;
    ListView1.Columns.Add;
    ListView1.Columns.Items[1].Caption := 'SUB TITLE';
    ListView1.Columns.Items[1].Width := 80;

    ListView1.Items.Add;
    ListView1.Items.Item[0].Caption := 'Short A';

    ListView1.Items.Add;
    ListView1.Items.Item[1].Caption := 'LONG LONG LONG LONG LONG LONG A';

    ListView1.Items.Add;
    ListView1.Items.Item[2].Caption := 'Short B';

    ListView1.Items.Add;
    ListView1.Items.Item[3].Caption := 'LONG LONG LONG LONG LONG LONG B';
end;

procedure TForm1.ListView1DrawItem(Sender: TCustomListView;
  Item: TListItem; Rect: TRect; State: TOwnerDrawState);
VAR stmp : STRING; iHeight : Integer; xRect : TRect;
begin
    IF Item.Index > 0 THEN
    BEGIN
        Rect.Top := ta_Bottom [Item.Index -1];
    END
    ELSE
    BEGIN
        Sender.Canvas.Brush.Color := clWhite;
        Sender.Canvas.Pen.Color   := clWhite;
        Sender.Canvas.FillRect(Sender.ClientRect);
    END;

    stmp       := Item.Caption + #0;
    Rect.Right := Rect.Left + Sender.Column[0].Width;
    iHeight    := DrawText(Sender.Canvas.Handle, PChar(stmp), -1, Rect, DT_CALCRECT or DT_WORDBREAK);

    Rect.Bottom            := Rect.Top + iHeight;
    ta_Bottom [Item.Index] := Rect.Bottom;
    Rect.Right             := Rect.Left + Sender.Column[0].Width;

    Sender.Canvas.Brush.Style := bsclear;
    Sender.Canvas.Brush.Color := ta_Color[Item.Index];
    Sender.Canvas.Pen.Color   := clWhite;
    Sender.Canvas.FillRect(Rect);

    DrawText(Sender.Canvas.Handle, PChar(stmp), -1, Rect, DT_WORDBREAK);
end;

end.

You can see that the long item text is wordwrapped. If you change the size of the column, and then force the form to repaint, by minimising and the restoring again, the test is redrawn correctly.

If you need to perform onColumnResize operations, then you can provide an event handler by following the following method :-

http://www.swissdelphicenter.com/en/showcode.php?id=1264

I hope this helps :-)
Steve
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wqclatreAuthor Commented:
Thanks. now I have something to start with
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.