Select a Single Item in a List View Control

This simple operation took me longer to figure out than I would like, the answer wasn’t obvious on msdn or in any of the searches I did so I figure I should just put it up here for anyone else who might look for it:

To select an item in a list view create an LVITEM structure, change a few of it’s fields and pass it to LVM_SETITEMSTATE:

// Select current item in the list
LVITEM item = {0};
item.mask = LVIF_STATE;
item.state = item.stateMask = LVIS_SELECTED;
SendMessage(mhProductList, LVM_SETITEMSTATE, mCurrentSettings, (LPARAM)&item);

Oh and if you’re trying to turn on whole line section you’ll need to use the extended styles:

// Set extended styles
SendMessage(hWndListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
This entry was posted in programming, windows. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>