Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: With a TListView, how do I get images in subitem columns?


This question is answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Mar 2, 2018 1:57 PM Last Post By: Remy Lebeau (Te...
Debbie Erickson

Posts: 12
Registered: 4/4/13
With a TListView, how do I get images in subitem columns?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 2, 2018 12:46 PM
Hi. I don't know how to get this image to print where I want it to print. It seems to have a mind of it's own. It ALWAYS prints in the first column and I haven't been able to print it where/when I want it to go. In my project, I need the image to print in the column 5, or in other words TListView1.Items[x].subitems[4].

I put down a TImageList control on the form, then added my bitmap. Next, I set the SmallImages property of the TListView to be the TImageList. As I was adding rows and columns (or subitems), when I got to subitem4, I added an image index. The code looks like TListView1.Items.subitemsimages[4]:= 0; This is for the first image in the Imagelist.

When I googled it, I saw that other people also complained about getting the image in column 0 instead of the column they intended. The responses all said it had to be a custom drawing. In column 5 I want to see both the text I assigned to it as well as the image I chose for it.

Can somebody point be to some working code that demonstrates printing an image, in a TListview, in a column other than the Caption column? I have defined a CustomDrawSubItem event, but it doesn't expose the TRect property of the canvas, so I can't do something like TListView1.Canvas.StretchDraw(rect, myImage).
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: With a TListView, how do I get images in subitem columns?
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 2, 2018 1:57 PM   in response to: Debbie Erickson in response to: Debbie Erickson
Debbie Erickson wrote:

Hi. I don't know how to get this image to print where I want it to
print. It seems to have a mind of it's own. It ALWAYS prints in the
first column and I haven't been able to print it where/when I want it
to go.

That is because the TListItem.ImageIndex property defaults to 0, and
you are using the wrong index when accessing the
TListItem.SubItemImages[] property.

In my project, I need the image to print in the column 5, or in other
words TListView1.Items[x].subitems[4].

The 5th column is SubItems[3], not SubItems[4]. TListItem.SubItems is
0-indexed, so the 2nd column is SubItems[0], and so on.

The code looks like TListView1.Items.subitemsimages[4]:= 0;

Should be using 3 instead of 4.

When I googled it, I saw that other people also complained about
getting the image in column 0 instead of the column they intended.

Then they likely weren't using the image indexes correctly, either.

The responses all said it had to be a custom drawing

That is not necessary, unless you need to customize the drawing, such
as moving it over a few pixels when grid lines are enabled.

In column 5 I want to see both the text I assigned to it as well as
the image I chose for it.

Works fine for me when I try it.

Can somebody point be to some working code that demonstrates printing
an image, in a TListview, in a column other than the Caption column?

You already are, you just need to fix your indexing.

I have defined a CustomDrawSubItem event, but it doesn't expose the
TRect property of the canvas, so I can't do something like
TListView1.Canvas.StretchDraw(rect, myImage).

Yes, you can, but you have to query the TRect manually. Use
ListView_GetSubItemRect() for that:

https://msdn.microsoft.com/en-us/library/windows/desktop/bb775004.aspx

ListView_GetSubItemRect() is declared in the Winapi.CommCtrl unit.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02