CListBox::CompareItem

Called by the framework to determine the relative position of a new item in a sorted owner-draw list box.

virtual int CompareItem( 
   LPCOMPAREITEMSTRUCT lpCompareItemStruct  
);

Parameters

  • lpCompareItemStruct
    A long pointer to a COMPAREITEMSTRUCT structure.

Return Value

Indicates the relative position of the two items described in the COMPAREITEMSTRUCT structure. It may be any of the following values:

Value

Meaning

–1

Item 1 sorts before item 2.

0

Item 1 and item 2 sort the same.

1

Item 1 sorts after item 2.

See CWnd::OnCompareItem for a description of the COMPAREITEMSTRUCT structure.

Remarks

By default, this member function does nothing. If you create an owner-draw list box with the LBS_SORT style, you must override this member function to assist the framework in sorting new items added to the list box.

Example

// CMyODListBox is my owner-drawn list box derived from CListBox. This  
// example compares two items using _tcscmp to sort items in reverse  
// alphabetical order. The list box control was created with the  
// following code: 
//   m_myODListBox.Create( 
//      WS_CHILD|WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL| 
//      LBS_SORT|LBS_MULTIPLESEL|LBS_OWNERDRAWVARIABLE|LBS_WANTKEYBOARDINPUT, 
//      CRect(10,250,200,450), pParentWnd, IDC_MYODLISTBOX); 
// 
int CMyODListBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{
   ASSERT(lpCompareItemStruct->CtlType == ODT_LISTBOX);
   LPCTSTR lpszText1 = (LPCTSTR) lpCompareItemStruct->itemData1;
   ASSERT(lpszText1 != NULL);
   LPCTSTR lpszText2 = (LPCTSTR) lpCompareItemStruct->itemData2;
   ASSERT(lpszText2 != NULL);

   return _tcscmp(lpszText2, lpszText1);
}

Requirements

Header: afxwin.h

See Also

Reference

CListBox Class

Hierarchy Chart

WM_COMPAREITEM

CWnd::OnCompareItem

CListBox::DrawItem

CListBox::MeasureItem

CListBox::DeleteItem

Other Resources

CListBox Members