共用方式為


CListCtrl::MapIndexToID

Maps the index of an item in the current list-view control to a unique ID.

UINT MapIndexToID(
     UINT index
) const;

Parameters

Parameter

Description

[in] index

The zero-based index of an item.

Return Value

A unique ID for the specified item.

Remarks

A list-view control internally tracks items by index. This can present problems because indexes can change during the control's lifetime. The list-view control can tag an item with an ID when the item is created. You can use this ID to access a specific item for the lifetime of the list-view control.

Note that in a multithreaded environment the index is guaranteed only on the thread that hosts the list-view control, not on background threads.

This method sends the LVM_MAPINDEXTOID message, which is described in the Windows SDK.

Requirements

Header: afxcmn.h

This control is supported in Windows Vista and later.

Additional requirements for this method are described in Build Requirements for Windows Vista Common Controls.

Example

The following code example defines a variable, m_listCtrl, that is used to access the current list-view control. This variable is used in the next example.

public:
    // Variable used to access the list control.
    CListCtrl m_listCtrl; 

The following code example demonstrates the MapIndexToID method. In an earlier section of this code example, we created a list-view control that displays two columns titled "ClientID" and "Grade" in a report view. The following example maps the index of each list-view item to an identification number, and then retrieves the index for each identification number. Finally, the example reports whether the original indexes were retrieved.

// MapIndexToID 
    int iCount = m_listCtrl.GetItemCount();
    UINT nId = 0;
    UINT nIndex = 0;
    for (int iIndexOriginal = 0; iIndexOriginal < iCount; iIndexOriginal++)
    {
        // Map index to ID.
        nId = m_listCtrl.MapIndexToID((UINT)iIndexOriginal);

        // Map ID to index.
        nIndex = m_listCtrl.MapIDToIndex(nId);

        if (nIndex != (UINT)(iIndexOriginal))
        {
            CString str;
            str.Format(_T("Mapped index (%d) is not equal to original index (%d)"),
                nIndex, (UINT)(iIndexOriginal));
            AfxMessageBox(str);
            return;
        }
    }
    AfxMessageBox(_T("The mapped indexes and original indexes are equal."), 
        MB_ICONINFORMATION);

See Also

Reference

CListCtrl Class

Hierarchy Chart

LVM_MAPINDEXTOID

CListCtrl::MapIDToIndex