Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 21: Tree and List Controls Visual C++ and MFC Fundamentals


lvItem.mask = LVIF_TEXT;
lvItem.iItem = 0;
lvItem.iSubItem = 0;
lvItem.pszText = "Sandra C. Anschwitz";
m_List.InsertItem(&lvItem);

lvItem.mask = LVIF_TEXT;
lvItem.iItem = 1;
lvItem.iSubItem = 0;
lvItem.pszText = "Roger A. Miller";
m_List.InsertItem(&lvItem);

lvItem.mask = LVIF_TEXT;
lvItem.iItem = 2;
lvItem.iSubItem = 0;
lvItem.pszText = "Marie-Julie W. Gross";
m_List.InsertItem(&lvItem);

lvItem.mask = LVIF_TEXT;
lvItem.iItem = 3;
lvItem.iSubItem = 0;
lvItem.pszText = "Ella Pius Roger";
m_List.InsertItem(&lvItem);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

The state member variable of the LVITEM structure specifies what to do with the new
item. For example, once the item has been added, you may want to prepare it for deletion
prior to a cut-and-paste operation, in which case you would give it a value of
LVIS_CUT. If the item is involved in a drag-and-drop operation, you can assign it a state
value of LVIS_DROPHILIGHTED. To give focus to the item, set its state value to
LVIS_FOCUSED. An item with an LVIS_SELECTED state value will be selected.

Besides the above version of the CListCtrl::InsertItem() method, the CListCtrl class
provides this other version:

int InsertItem(int nItem, LPCTSTR lpszItem );

This is a good simplification of the earlier version. The nItem argument is the index of
the new item to de added. Like the LVITEM::iItem member variable, the value of this
Free download pdf