?
Solved

Multiple lines in one row of a listview?

Posted on 2003-03-06
2
Medium Priority
?
203 Views
Last Modified: 2010-04-04
Can someone tell me how I can add Multiple lines in one row of a listview?
0
Comment
Question by:wqclatre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
steve_hsk earned 400 total points
ID: 8087537
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
 
LVL 2

Author Comment

by:wqclatre
ID: 8179364
Thanks. now I have something to start with
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question