Setting ListViewItem check state to indeterminate check

  • Thread starter Thread starter Annette Miller
  • Start date Start date
A

Annette Miller

Hi All,

Wondering if anyone can help me here.

I want to be able to progmatically set an item check state in a list view to
the indeterminate (i.e. grayed in Windows Classic/Filled Square with XP
theme) state.

I realise i can do this in the Item_Checked event but I want to be able to
set it progmatically depending on how many items are selected in another
listview.

I'm using VS2005 Beta 2 if it makes any difference.

Cheers.
 
Since the default listview doesn't have tri-state checkbox support
(or at least it doesn't in 1.1) you'll need to handle the state images
manually.

1. Create state images for your three different states and add
them to an imagelist
2. Add the imagelist as the StateImageList for the listview
3. Turn of checkboxes in your listview
4. Set the StateImageIndex property according to what state
the item should have.

Note that the ListViewItem.Checked, ListView.CheckedItems
and ListView.CheckedIndices properties will be useless
and you'll need to check the StateImageIndex to determine
the state of an item.

Another approach that *might* work is to enable the CheckBoxes
property and then add another stateimage to the generated
stateimagelist. Then set the stateimageindex manually when you
want the indeterminate image. This should enable the usual
checkbox handling to still work (along with the properties
mentioned above)

The ItemCheck event is of no use to you. It is used by both the
ListView and CheckedListBox, but only CheckedListBox
supports setting NewValue = Indeterminate
The Windows Forms team seems to have realized the mistake
of using the same eventarg class for both of these controls so
they added a new event (ItemChecked) to the ListView class
that doesn't have the NewValue member in it's eventargs (not
very helpful for you though).

/Claes
 
Claes,

Thanks for your thoughts.

I tried both your suggestions and neither of them work in a satisfactory
fashion.

Using my own ImageList worked to a certain extent. Say in the state
imagelist 0 is unchecked, 1 is checked and 2 is indeterminate. With
checkboxes = true, it will go unchecked, checked, indeterminate. The problem
is that no ItemCheck event is raised between 1 to 2, but it is raised again
when it goes from 2 to 0. What's with that? The main problem I have here is
I don't want users to be able to set the item to indeterminate - I want to
set it if they haven't met the criteria.

I could disable checkboxes and use stateimages and manipulate it manually
but then the user can't check or clear the checkbox - totally negating the
point of having it there in the first place.

Any other suggestions?

Regards,
Ann
 
Annette Miller said:
Claes,

Thanks for your thoughts.

I tried both your suggestions and neither of them work in a satisfactory
fashion.

Using my own ImageList worked to a certain extent. Say in the state
imagelist 0 is unchecked, 1 is checked and 2 is indeterminate. With
checkboxes = true, it will go unchecked, checked, indeterminate. The
problem is that no ItemCheck event is raised between 1 to 2, but it is
raised again when it goes from 2 to 0. What's with that? The main problem
I have here is I don't want users to be able to set the item to
indeterminate - I want to set it if they haven't met the criteria.

Interesting, I wouldn't have exepected it to actually make the transition
from checked to indeterminate automatically when you clicked on it. Seems to
be some unexpected behaviour with this method.
I could disable checkboxes and use stateimages and manipulate it manually
but then the user can't check or clear the checkbox - totally negating the
point of having it there in the first place.

Yepp, that is a problem. You'll need to handle mouse clicks yourself. Not
that hard to do actually (check the HitTest method, available in 2.0), but
it requires some work to get everything in order. Override OnMouseDown and
check where the user clicked using the HitTest method. If user clicked on
state image you call OnItemCheck (and perhaps OnItemChecked) and make the
transition accordingly.

As I stated before you also need to do something to get
CheckedItemsCollection and so on fully working. If you have full control
over the source code and act according to the way you know it works (i.e by
not calling those properties/methods), you can of course disregard that
problem.


/claes
 
Back
Top