0

While having a Listview with checkboxes, how do you programatically set all items to be checked?

AlexandruC
  • 3,527
  • 6
  • 51
  • 80

1 Answers1

3

Listview checkbox state is set through the state image bits of the item state. You can change item states with the LVM_SETITEMSTATE message, and can apply a change to all items by passing -1 as the index.

// The state-image index values:
//  1 for the "unchecked" (cleared) state-image
//  2 for the "checked" state-image

int iState = 2;
LVITEM lvi;
lvi.stateMask = LVIS_STATEIMAGEMASK;
lvi.state = INDEXTOSTATEIMAGEMASK(iState);
SendMessage(hwndListView, LVM_SETITEMSTATE, -1, (LPARAM)&lvi);
Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • 1
    Or, shorter, using `CListViewCtrl::SetItemState` – Roman R. Jul 11 '13 at 20:38
  • any ideas on this http://stackoverflow.com/questions/18336005/wtl-multithreading-multiple-interfaces-libraries – AlexandruC Aug 20 '13 at 13:13
  • And what about `lvi.iItem`? Shouldn't it also be set to `-1`? – Bart Mar 19 '15 at 13:26
  • @Bart: The index is given in the `wParam` message parameter. See https://msdn.microsoft.com/en-us/library/windows/desktop/bb761196%28v=vs.85%29.aspx. – Jonathan Potter Mar 19 '15 at 20:04
  • @JonathanPotter Yes, but there's also the struct member. It is undocumented whether `wParam` and `iItem` should have the same value, and what happens if they don't. `#define ListView_SetItemState(hwnd,i,pitem) (BOOL)SNDMSGA((hwnd),LVM_SETITEMSTATE,(WPARAM)(UINT)(i),(LPARAM)(LPLVITEMA)(pitem))` Again, the index `i` is set, but we have no information about `iItem`. I was wondering because I ran into some trouble with multiselection, when the values were inconsistent, but I am unsure whether this was the actual reason. – Bart Mar 20 '15 at 09:19
  • @Bart It's not undocumented. The docs clearly say that for this message only the `state` and `stateMask` fields are used. – Jonathan Potter Mar 20 '15 at 21:35
  • @JonathanPotter Ah, yes, the other members are ignored. Excuse me, I should have read it more carefully. – Bart Mar 23 '15 at 09:55