Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


When creating the list, its items are sorted in alphabetical order using the items text as
reference. Even if you add items later on, they are inserted in the appropriate order. This
sorting feature is controlled at design time by the Sort combo box box. By default, the
items of a list control are sorted in alphabetical order using the Ascending value or the
LVS_SORTASCENDING style. If you want items to be sorted in reverse alphabetical
order, set this property to Descending or create the control with the
LVS_SORTDESCENDING style.

21.3.3..Items of a List Control........................................................................


After visually adding or dynamically creating a list control, the next action you probably
would take is to populate the control with the desired items. This can be taken care of by
calling the CListCtrl::InsertItem() method. One of its syntaxes is as follows:

int InsertItem(const LVITEM* pItem );

This version requires an LVITEM pointer as argument. The LVITEM structure is
defined as follows:

typedef struct _LVITEM {
UINT mask;
int iItem;
int iSubItem;
UINT state;
UINT stateMask;
LPTSTR pszText;
int cchTextMax;
int iImage;
LPARAM lParam;
#if (_WIN32_IE >= 0x0300)
int iIndent;
#endif
} LVITEM, FAR *LPLVITEM;

The mask member variable is used to specify the types of values you want to set for the
current item.

The value of iItem specifies the index of the item that is being changed. The first item
would have an index of 0. The second would be 1, etc. The iSubItem member variable is
the index of the sub item for the current value. If the current item will be the leader, the
iSubItem is stored in a 0-based array. If it is a sub item, then it is stored in a 1 - based
array.

The pszText member variable is the string that will display as the item. You can specify
the length of this text by assigning a value to the cchTextMask variable.

After initializing the LVITEM variable, you can pass it to the InsertItem() method to
add it as a new item to the list. Here is an example that creates items and displays as a
List view:

BOOL COthersDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
LVITEM lvItem;
Free download pdf