I am using Delphi® for Win32® Version 11.0.2709.7128 on XP.
I would like to use a tListView with OwnerData = True and Checkboxes = True.
Unfortunately, when doing this the Checkboxes disappear yet the allocated
space for them remain (though clicking on the space does nothing).
Is this supposed to happen? I cannot find any documentation stating that you
cannot use Checkboxes when OwnerData = True.
NOTE: I have two columns in the listview and when I use it with OwnerData =
False it all works fine however when I change it to an OwnerData = True
listview (and supply the data through an OnData event) the data looks fine
and everything else works fine, just the listboxes disappear.
Any suggestions?
Thanks,
Michael.
> Unfortunately, when doing this the Checkboxes disappear yet
> the allocated space for them remain (though clicking on the space
> does nothing). Is this supposed to happen?
Yes. Checkboxes are implemented as images in an internal state image list.
When using OwnerData=True, you are responsible for providing the correct
state indexes for each list item when requested by the ListView. Are you
doing that much yet?
Gambit
> do I need to supply an ImageList
No. Enabling the CheckBoxes property causes the ListView to create its own
image list internally.
> If I use "Item.StateIndex" I must set StateImages to an
> ImageList prior to using
No, you don't.
> how do I set this to the "internal state image list"?
You don't. The ListView handles that for you.
> Otherwise I assume I use a tImageList and set StateImages to
> that
Only if you want to use custom images. In which case, you can't set the
CheckBoxes property to true.
> which means that I must populate the ImageList, does Delphi
> supply the "unchecked box" image and the "checked box"
> image somewhere that I can use?
No (well, yes, but not in a way that is OS neutral, so it won't look natural
on newer OS versions). You can use the DrawFrameControl() function to
render the various checkbox states to a temporary TBitmap, though.
> Or do I just set the state of the item?
Yes.
Gambit
Thanks again.
I tried setting the State in CustomDrawItem to -
State := State + [cdsChecked] and no joy ~ obviously a read parameter (no
var).
Note: I have already set "Item.Checked" to True
How do I go about setting state to "checked" and "unchecked" to get the
boxes to display?
Thanks,
Michael.
"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message
news:46e74457$1...@newsgroups.borland.com...
> I tried setting the State in CustomDrawItem to -
> State := State + [cdsChecked] and no joy ~ obviously
> a read parameter (no var).
You can't change the State parameter, correct.
> Note: I have already set "Item.Checked" to True
That won't work. Further investigation reveils that a ListView control with
the LVS_OWNERDATA style applied (which is what the OwnerData property does)
cannot use state images at all. So you are out of luck trying to use the
CheckBoxes and OwnerData properties together. You will have to owner-draw
the ListBox items manually to draw your own checkbox images.
Gambit
Thanks for all your help, all working now.
However I did not go down the OwnerDraw route, instead I created two small
images, one checked the other unchecked and put them in an ImageList which
was then used as the SmallImages.
I toggle the check state in my external list's record for the particular
item in the OnClick event.
I then set the ImageIndex in the OnData event.
Seems to work OK.
Again, many thanks for all the effort you put in, without knowing that you
cannot do it the other way I could have wasted a lot of time trying many
different combinations.
Michael.
"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message
news:46e7aed3$1...@newsgroups.borland.com...
Start by creating a TImageList component and assigning it to the StateImages
property of the TListView. You must do this before you set the OwnerData
property of the TListView to TRUE. At this point, the TImageList is
populated
with the Unchecked (0) and Checked (1) state images.
Now set the OwnerData property of the TListView to TRUE. The state images
remain in the image list even though the control is no longer visually
maintaining
the checked state. You can verify this by setting the StateIndex property
to
0 or 1 in the OnData event of the TListView.
Next, trap the OnMouseDown event for the TListView and call the
GetHitTestInfoAt
method of the TListView. If htOnStateIcon is in the HitTestInfo then you
know
the mouse click was on the state icon. Call the GetItemAt method of the
TListView to get the specific item, extract the ItemIndex and now you can
update the checked state in your virtual list item.
Now you need to force the TListView to update the changed item. The
simplest
way is to invalidate the entire TListView control but this causes some
flicker.
To get rid of the flicker, invalidate only the changed list item. To do
this, call the
DisplayRect method of the TListItem and pass it drBounds as the DisplayCode.
Then call InvalidateRect, passing in the handle of the TListView and the
item's
display rectangle.
Q.E.D.
"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message
news:46e7aed3$1...@newsgroups.borland.com...
> To get rid of the flicker, invalidate only the changed list item.
> To do this, call the DisplayRect method of the TListItem and
> pass it drBounds as the DisplayCode. Then call InvalidateRect,
> passing in the handle of the TListView and the item's display
> rectangle.
Or simply call TListView's UpdateItems() method instead.
Gambit
But I found some profound weirdness with the TImageList component.
I went back into the test project and the state icons were missing
from the TImageList component. I was able to reproduce getting the
check boxes back but I have to say TImageList is bizarre.
In the hope that this helps someone, here are the exact steps to
get this working (I'm using BDS2006):
Drop a TListView component on the form
Set the ViewStyle property to vsReport
Add at least one column using the Columns editor
Set the Checkboxes property to true
Drop a TImageList component on the form
Set the StateImages property to the TImageList you just dropped
Set the OwnerData property to true
Open the TImageList editor, verify that it has 2 icons and close the editor
Delete the StateImages property (break the link)
Open the TImageList editor, verify that it has 2 icons and close the editor
At runtime, set the StateImages property to the TImageList component
WARNING: If you don't break the link before you save the project, the icons
will go missing. This is a serious WTF.
More weirdness: The TImageList editor has an export function but no import.
Damned if I can figure out how you would use the exported bitmap.
Even more weirdness: I used the TImageList editor to add a 16x16 bitmap
(16 colors) and it resets the TImageList to 5x5. Why 5x5?? You got me.
"TCustomImageList.InsertImage" and "TCustomImageList.InsertIcon"
contain the following code:
TempList := TCustomImageList.CreateSize(5, 5);
So this is where the 5x5 comes from - but I still have no idea why.
Another WTF.
Erik Turner
"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message
news:4766c5de$1...@newsgroups.borland.com...