TListView with checkboxes and OwnerData

I have a simple TListView, set to ViewStyle=vsReport.
I have Checkboxes=true, which works without problems so far.

Now I want to use OwnerData, so I implemented my OnData event.
The data is showing perfectly. Except one thing: the checkboxes are not showing.

To reproduce:
- Add TListView
- Set ViewStyle=vsReport
- Set Checkboxes=true
- Set OwnerDraw=true
- In form init code, set ListView1.Items.Count=10
- In ListView1OnData, set Item.Caption := 'Hello World'

Does anyone know how I can use checkboxes & ownerdraw in a standard TListView?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
Drawing checkboxes in a listview or other screen item with a canvas is quite easy. Use the Windows API function DrawFrameControl() to draw it for you. The function requires several parameters:
DC: HDC -> The handle of the canvas where you want to draw the checkbox.
Rect: TRect -> The location and size of the checkbox.
uType: UINT -> The type of control you want to draw. Use DFC_BUTTON for a checkbox.
uState: UINT -> The state of the control. Use DFCS_BUTTONCHECK for a clear checkbox and use DFCS_BUTTONCHECK or DFCS_CHECKED for a checked checkbox. Add DFCS_INACTIVE to make it appear as inactive.

Use the OnDraw events to draw these controls on your listview and the OnClick or whatever other method to check/uncheck them.
ccrdudeAuthor Commented:
Thanks! But... I found an error in what I wrote above. I wrote twice OwnerData, but one time accidently OwnerDraw...
I actually don't want/need OwnerDraw, just OwnerData. Does your answer mean that I have to use OwnerDraw to draw the Checkbox if I use OwnerData, or were you just mislead by my mistake in mentioning OwnerDraw?

(anyway, the function you referred to may come in handy in another situation... I wish I had known it a year ago :) )
Wim ten BrinkSelf-employed developerCommented:
Oh, no OwnerDraw? Okay... If I'm not mistaken then when OwnerData is true, the Delphi component will not set the checkbox of the underlying Windows control. So I checked the MSDN site about the LVS_OWNERDATA value for virtual listviews. It says, quote:
"A virtual list view is a list-view control that has the LVS_OWNERDATA style. This style enables the control to handle millions of items because the owner receives the burden of managing item data. This allows you to use the virtual list-view control with large databases of information, where specific methods of data access are already in place.
A virtual list-view control maintains very little item information itself. Except for the item selection and focus information, the owner of the control must manage all item information. Other processes request item information from the owner by using LVN_GETDISPINFO notification messages.
Because this type of list control is intended for large data sets, it is recommended that you cache requested item data to improve retrieval performance. The list view provides a cache-hinting mechanism to assist in optimizing the cache. The hint is implemented in the form of an LVN_ODCACHEHINT notification message." for the complete overview...

Thus, it is unlikely that a virtual listview will be able to maintain the state of any checkbox on it. You'll have to draw them yourself, I guess...

I was not mistaken by that OwnerData/OwnerDraw thingie, btw. I knew I had looked at this myself once, a long while ago. I just could not remember why I used OwnerDraw to draw the checkbox. Now I know... :-)
You can also use that function to draw other kinds of controls, btw...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ccrdudeAuthor Commented:
If the listview already gets text & image etc. using OnData, why not the checkbox state?... like TVirtualStringTree does, which I now use since I already have it in other places in the project. But thanks for your detailed description, which I'Ve stored for future reference :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.