Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Multiple lines in one row of a listview?

Can someone tell me how I can add Multiple lines in one row of a listview?
0
wqclatre
Asked:
wqclatre
1 Solution
 
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
 
wqclatreAuthor Commented:
Thanks. now I have something to start with
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now