Disable Checkbox ListViewItem

G

Guest

How can I disable a listviewitem which is basically a checkbox. Suppose I
have 10 checkboxes in my ListView and I want three of them disabled so that
the user can not check/uncheck those three items.

I am using C#.

Thanks in advance for your help.
 
C

Claes Bergefall

A checkbox is in reality simply 2 different state images (one for the
checcked box and one for the unchecked box) that are taken from a system
supplied image list and applied to the listview item as appropriate.
Unfortunately .NET doesn't provide an event for when the state image index
changes so you'll have to override WndProc and handle the LVN_ITEMCHANGING
notification. Here's code that does most of this (in VB.NET):

http://groups.google.se/group/micro...dowsforms.controls/msg/5afb5ede99eb3ffc?hl=sv

However, this code checks for changes to the selected state flag
(LVIS_SELECTED). Change this to check for the new state image index instead
(different bits in the state parameter) and return 1 to prevent the change
(0 to allow it). The state image index is 1 for the unchecked box and 2 for
the checked one. If you set it to 0 the checkbox will be removed

Read more using the following keywords: LVITEM, INDEXTOSTATEIMAGEMASK,
LVN_ITEMCHANGING, NMLISTVIEW

The following code should give you the state image index provided the code
in the link above (still in VB.NET):
((nmlv.uNewState And &HF000) >> 12)

/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top