views:

294

answers:

1

In a Win32 application I have a dialog with a list control which is defined is the dialog template:

CONTROL "",IDC_LIST_Attributes,"SysListView32",LVS_REPORT | 
    LVS_SINGLESEL | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP,7,36,246,110

In the runtime I retrieve the handle to that control and perform different operations with it - remove all items, add items, etc. It works fine.

The problem is I can't programmatically mark an item as selected. I use the following code for that:

LVITEM lvItem;
lvItem.stateMask = stateMask;
lvItem.state = state;
SendMessage( windowHandle, LVM_SETITEMSTATE, indexToSelect, (LPARAM)&lvItem);

This code runs and no changes happen to the list control. when I clisk on items with a mouse they are selected allright. What am I missing?

+1  A: 

Have you tried ListView_SetItemState Macro?

From the MSDN Link:

Items will only show as selected if the list-view control has focus or the LVS_SHOWSELALWAYS style is used.

Another Link that my help.

Aamir
Tried just now - the same problem. In fact it even expands into equivalent source code.
sharptooth
Well, with the style set it got better - the selection bar at least retained. But when I retrieved the current selection ( LVM_GETSELECTIONMARK) it returned CB_ERR (no selection). So I both change the item state and send LVM_SETSELECTIONMARK. Now it works as needed.
sharptooth