CComboBox::CompareItem

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

virtual int CompareItem(
   LPCOMPAREITEMSTRUCT lpCompareItemStruct 
);

Parameters

Return Value

Indicates the relative position of the two items described in the COMPAREITEMSTRUCT structure. It can 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 COMPAREITEMSTRUCT.

Remarks

By default, this member function does nothing. If you create an owner-draw combo 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

// CMyComboBox is my owner-drawn combo box derived from CComboBox. This 
// example compares two items using strcmp to sort items in reverse 
// alphabetical order. The combo box control was created with the 
// following code:
//   pmyComboBox->Create(
//      WS_CHILD|WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL|
//      CBS_SORT|CBS_OWNERDRAWVARIABLE,
//      myRect, pParentWnd, 1);
//
int CMyComboBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{
   int iComp = 0;
   ASSERT(lpCompareItemStruct->CtlType == ODT_COMBOBOX);
   LPCTSTR lpszText1 = (LPCTSTR) lpCompareItemStruct->itemData1;
   ASSERT(lpszText1 != NULL);
   LPCTSTR lpszText2 = (LPCTSTR) lpCompareItemStruct->itemData2;
   ASSERT(lpszText2 != NULL);

   if (NULL != lpszText1 && NULL != lpszText2)
   {
      iComp = _tcscmp(lpszText2, lpszText1);    
   }

   return iComp;
}

Requirements

Header: afxwin.h

See Also

Concepts

CComboBox Class

CComboBox Members

Hierarchy Chart

WM_COMPAREITEM

CComboBox::DrawItem

CComboBox::MeasureItem

CComboBox::DeleteItem