You are currently offline, waiting for your internet to reconnect

How To Select a Listview Item Programmatically

Selecting a listview item is not as easy as selecting a list box item wasin Windows version 3.1. To select a list box item, an application sends anLB_SETCURSEL or LB_SETSEL to a single- or multiple-selection list boxrespectively. To select a listview item, an application sends anLVM_SETITEMSTATE message or calls the ListView_SetItemState() macro.
More information
An application can force a selection of a listview item. You might want theapplication to do this when a user clicks a column other than the firstcolumn of a listview of multiple subitems or columns.

Currently, a listview item is selected only when the user clicks the firstcolumn of that item. However, you many want the application to select theitem regardless of which column in the listview is clicked.

Windows does not provide a separate message or function to set thecurrent selection in a listview. Instead, it defines item states or LVIS_*values that determine the listview item's appearance and functionality.LVIS_FOCUSED and LVIS_SELECTED in particular are the states that determinea listview item's selection state.

To select a listview item programmatically, an application sets thelistview item's state as follows:
   ListView_SetItemState (hWndListView,         // handle to listview                          iWhichItem,         // index to listview item                          LVIS_FOCUSED | LVIS_SELECTED, // item state                          0x000F);                      // mask				
Note that the last parameter passed to this macro is a mask specifyingwhich bits are about to change. LVIS_FOCUSED and LVIS_SELECTED are definedin <commctrl.h> as 0x0001 and 0x0002 respectively, so you need to set thelast four bits of the mask.

The same principle applies to selecting a treeview item programmatically.The only difference is that an application sends a TVM_SETITEM message orcalls the TreeView_SetItem() macro.

Because listviews allow multiple selection by default, you can program anapplication to select multiple items by simulating a CTRL keydown (or SHIFTkeydown event) prior to setting the item state. For example, the followingcode simulates the pressing of the CTRL key:
   BYTE  pbKeyState [256];   GetKeyboardState ((LPBYTE)&pbKeyState);   pbKeyState[VK_CONTROL] |= 0x80;   SetKeyboardState ((LPBYTE)&pbKeyState);<BR/>				
Note that if an application simulates a keypress, it must also beresponsible for releasing it by resetting the appropriate bit. For example,the following code simulates the release of a CTRL key:
   BYTE  pbKeyState [256];   GetKeyboardState ((LPBYTE)&pbKeyState);   pbKeyState[VK_CONTROL] = 0;   SetKeyboardState ((LPBYTE)&pbKeyState);				
Similarly, retrieving the currently selected item in a listview controlin Windows is not as easy as sending an LB_GETCURSEL message to alistbox control.

For listviews, call the ListView_GetNextItem() function with theLVNI_SELECTED flag specified:
   iCurSel = ListView_GetNextItem (ghwndLV, -1, LVNI_SELECTED);				
For treeviews, retrieve the currently selected item by calling theTreeView_GetNextItem() function with the TVGN_CARET flagspecified or by calling the TreeView_GetSelection() macro directly:
   iCurSel = TreeView_GetNextItem (ghwndTV, NULL, TVGN_CARET);				
   iCurSel = TreeView_GetSelection (ghwndTV);				

Article ID: 131284 - Last Review: 06/22/2014 17:56:00 - Revision: 3.0

  • kbctrl kbhowto kblistview KB131284
© 2016 Microsoft