PDA

View Full Version : multiline text in listview subitems


muttski
02-21-2006, 03:26 AM
hi.
does anyone know how i can display more than one line in in a listview subitem caption?

i want my listview to look somthing like this:

------------------------------------
item: | subitem: | subitem2
------------------------------------
| line1 | bla bla
test | line2 | bla bla
| line3 | bla bla
------------------------------------

ctiberg
02-21-2006, 03:29 AM
You could set the Listview to ownerdraw and draw the items yourself.

Best regards,
Christian Tiberg

PS. Please be sure to click "Accept as answer" if this helped you! DS.

PPS. I do not answer forum messages by email! DS.

krjt
02-21-2006, 03:30 AM
you have a solution in the following thread
http://www.delphipages.com/threads/thread.cfm?ID=158560&G=158526

I hope this helps you, if yes please accept as answer I always accept the helping answer.

cheers,
krjt

muttski
02-21-2006, 03:36 AM
well i am usinga listview component so that stuff in that thread woult help me much i think

about the ownerdraw could you kame me a sample of how i do this and will it work hen i sort my listview later ?

krjt
02-21-2006, 05:42 AM
Here is an example implementation. Don't forget to set the listbox style to lbOwnerDrawVariable.

procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Items.Add('First line'+#13+'First second line');
ListBox1.Items.Add('Second line'+#13+'Second second line');
end;

procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
var Height: Integer);

var
ItemString: string;
MyRect: TRect;
MyImage: TImage;
MyList: TListBox;
begin
// Don't waste time with this on Index = -1
if (Index > -1) then
begin
MyList := TListBox(Control);
// Create a temporary canvas to calculate the height
MyImage := TImage.Create(MyList);
try
MyRect := MyList.ClientRect;
ItemString := MyList.Items.Strings[Index];
MyImage.Canvas.Font := MyList.Font;
// Calc. using this ComboBox's font size
Height := DrawText(MyImage.Canvas.Handle, PChar(ItemString),
- 1, MyRect, DT_CALCRECT or DT_WORDBREAK or DT_CALCRECT);
finally
MyImage.Free;
end;
end;
end;


procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
ItemString: string;
begin
TListBox(Control).Canvas.FillRect(Rect);
ItemString := TListBox(Control).Items.Strings[Index];
DrawText(TListBox(Control).Canvas.Handle, PChar(ItemString), - 1, Rect, DT_WORDBREAK);
end;

I hope this helps you, if yes please accept as answer I always accept the helping answer.

cheers,
krjt

AceOmega
02-21-2006, 06:09 AM
Can you use the Columns? Here from Delphi help...

This example requires a TListView, a TImageList and a TComboBox. You will need to double click the image list and add several images to the image list prior to running the project. You can use *.bmp or *.ico files from the \Images\Icons directory.
During the formís OnCreate event handler, items for the List View control are created for each image in the Image List and the ImageIndex is assigned the number of the image within the ImageList. Two columns are created so that when ViewStyle is vsReport, you will have columns to view.

Also within the formís OnCreate event handler assign the ComboBox each of the four TViewStyle constants to the Itemsí Objects property. You could also simply code this within a series of OnClick event handlers as, for instance, ListView1.->ViewStyle := vsIcon.

procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
ListItem: TListItem;
NewColumn: TListColumn;
begin
// Create a ListView item for each image in the ImageList
with ListView1 do
begin
SmallImages := ImageList1;
LargeImages := ImageList1;
for I := 0 to ImageList1.Count - 1 do
begin
ListItem := Items.Add;
Listitem.Caption := 'Image' + IntToStr(I);

ListItem.ImageIndex := I;
end;
// Create two columns to show during viewing as vsReport
NewColumn := Columns.Add;
NewColumn.Caption := 'Column 1';
NewColumn := Columns.Add;
NewColumn.Caption := 'Column 2';
// Add View styles and constants to the Combo Box
ComboBox1.Items.AddObject('vsIcon', TObject(vsIcon));
ComboBox1.Items.AddObject('vsList', TObject(vsList));
ComboBox1.Items.AddObject('vsReport', TObject(vsReport));

ComboBox1.Items.AddObject('vsSmallIcon', TObject(vsSmallIcon));
// Display first item in the Combo Box
ComboBox1.ItemIndex := 0;
end;
end;

procedure TForm1.ComboBox1Click(Sender: TObject);
begin
with ComboBox1 do
ListView1.ViewStyle := TViewStyle(Items.Objects[ItemIndex]);
end;

muttski
02-21-2006, 06:10 AM
i am using a LISTVIEW not a listbox,
and its my subitems i want to splitt up in lines so i dont se how this is going to help me....
but thanks for trying to help

krjt
02-21-2006, 06:21 AM
oops sorry for the misconception...

I hope this helps you, if yes please accept as answer I always accept the helping answer.

cheers,
krjt

muttski
02-21-2006, 06:24 AM
lol it looks like i didnt explain my problem corectly....
what i want is to make a listview look somthing like this:
http://muttski-software.com/listview.jpg

what i want is to make my subitems text splitted in 3 lines like the image shows

and to show you how my looks right now heres an image of my listview :
http://muttski-software.com/listview2.jpg

so if you look at mine you se my text is very long and that is what i want to fix

AceOmega
02-21-2006, 06:33 AM
Oh, ok you want the column to Word Wrap. It does not appear that this component supports Word Wrap. You could try making sure that the Column.Autosize := False; and set your .MaxWidth but this will probably just truncate your text. I would suggest getting a third party ListView. Lots of free ones out there which may save you hours of coding. Sorry I could not be of more help.

muttski
02-21-2006, 06:50 AM
ok..
but i dont want it to use word warp i want it display 3 difrent lines under each other like this:

user:me
lvl:12
chname:somthing

with word warp it will only get messed up and maybe look like this:

user:me lv
l:12 chnam
e:somthing

if this is not posible with the normal listview do you know about a component that do suports this or somthing simular

AceOmega
02-21-2006, 06:57 AM
Word Wrap always raps on CRs or #13 and hole wards. It will never wrap in the middle of a word. But, if you want to ensure it wraps like...

user:me
lvl:12 chnam
e:somthing

then put in CR when puttin in your text like...

:= 'user:me'+#13+'lvl:12 chnam'+#13+'e:somthing';

This will ensure it wraps the way you want. As for a third party component I do not know of one off the top of my head. Just go to Google.Com and search for 'TListView for Delphi'.

AceOmega
02-21-2006, 07:14 AM
Here is one...

http://teachme.tuwien.ac.at/lo/replist.html

not sure if they have Word Wrap though.

The other solution is to do it your self but the code can get quite messy. I did something like this with a TreeView on the Resize event which looked like this...

{Set max number of chars in each line and word wrap}
Buffer := SummaryText;
CharNumber := 120;
While (Sender.Canvas.TextWidth(Buffer)
>
(Sender.Width-70))
And
(CharNumber > 25) do
begin
CharNumber := CharNumber - 1;
Buffer := WrapText(SummaryText, #13#10, ['.',' ',#9,'-'], CharNumber);
If Pos(chr(13),Buffer) > 0 then
begin
Buffer := Copy(Buffer, 0,
Pos(chr(13),Buffer)-1);
end;
end; (**)
SummaryText := WrapText(SummaryText, #13#10, ['.',' ',#9,'-'], CharNumber);

{Insert Data into Tree}
WordRapLenght := 0;
Buffer := SummaryText;
While WordRapLenght < Length(SummaryText) do
begin {Add Data to Tree}
If Pos(chr(13),Buffer) > 0 then
begin
Buffer := Copy(Buffer,
0,
Pos(chr(13),Buffer)-1);
end;

WordRapLenght := WordRapLenght + Length(Buffer) + 2;
Sender.Items.AddChild(Sender.Selected,Buffer);
Sender.Selected.Expanded := True;
Buffer := Copy(SummaryText,
WordRapLenght + Pos(chr(13),Buffer) + 1,
Length(SummaryText));
end; {Add Data to Tree}

Like I said messy.