Microsoft KB Archive/162733

= Microsoft Knowledge Base =

How To Use Masks to Set/Get Item States in CListCtrl
Last reviewed: July 2, 1997

Article ID: Q162733

4.00 4.10 4.20 Windows kbprg kbcode kbhowto

The information in this article applies to:


 * The Microsoft Foundation Classes (MFC), included with: - Microsoft Visual C++ for Windows, versions 4.0, 4.1, 4.2

SUMMARY
The CListCtrl class does not provide any explicit function calls to set or retrieve the states of the list items. This functionality is handled with flags or masks. This article explains how to use these masks to access list items and change their states.

MORE INFORMATION
The CListCtrl class supports the GetItemState, SetItemState, and GetNextItem functions. When you use GetNextItem with the appropriate mask, you should be able to locate the desired list item by its state. To change the state, call GetItemState on the item you have located to fill the LV_ITEM structure, make modifications to this structure, and call SetItemState to pass the structure back to your control.

GetNextItem
The GetNextItem function takes two parameters. The first indicates the item (0 based) in the list after which you need to begin your search. The current item is excluded from the search. To search the entire list, supply a -1 for this parameter.

The second parameter indicates the state that identifies the item you are searching for. For the CListCtrl, the flags all begin with LVNI_. (See the documentation on CListCtrl::GetNextItem for a complete list.)

GetItemState and SetItemState
The GetItemState function returns a UINT indicating current state flags of the indicated list item. SetItemState accepts a UINT to set the state flags of the indicated list item. Both of these functions take a UINT mask that indicates which of the state flags should be affected.

Sample Code
The following code demonstrates how to access several CListCtrl items based on their state. If you want to set the Selected and Focus states of a certain list item in your OnInitialUpdate, do the following:

NOTE: This code assumes you have a member variable associated with a list control (m_LC) on your view. The CListCtrl should have several elements, and at least one of these elements should begin with the letter "A".

void CMyView::OnInitialUpdate {    BOOL b;     int iItem, nFlags; UINT nState, nMask; CString strText;

iItem = -1; // Search from the beginning of the list. nFlags = LVNI_ALL; // Search all subsequent list items.

iItem = m_LC.GetNextItem( iItem, nFlags );

while ( iItem > -1 ) // As long as GetNextItem found an item... {      strText = m_LC.GetItemText( iItem, 0 ); if ( strText[0] == 'A' ) {                // ...we will retrieve and change the state of our item. nMask = LVIF_STATE; // Get the item's state. nState = m_LC.GetItemState( iItem, nMask ); // Add the SELECTED and FOCUSED flags. nState |= (LVIS_SELECTED | LVIS_FOCUSED); // Set the flags back, only affects the State flags. b = m_LC.SetItemState( iItem, nState, nMask ); }    }