The framework calls this member function to specify the relative position of a new item in a child sorted owner-draw combo or list box.

afx_msg int OnCompareItem( 
   int nIDCtl, 
   LPCOMPAREITEMSTRUCT lpCompareItemStruct  


The identifier of the control that sent the WM_COMPAREITEM message.


Contains a long pointer to a COMPAREITEMSTRUCT data structure that contains the identifiers and application-supplied data for two items in the combo or list box.

Indicates the relative position of the two items. It may be any of the following values:




Item 1 sorts before item 2.


Item 1 and item 2 sort the same.


Item 1 sorts after item 2.

If a combo or list box is created with the CBS_SORT or LBS_SORT style, Windows sends the combo-box or list-box owner a WM_COMPAREITEM message whenever the application adds a new item.

Two items in the combo or list box are reformed in a COMPAREITEMSTRUCT structure pointed to by lpCompareItemStruct. OnCompareItem should return a value that indicates which of the items should appear before the other. Typically, Windows makes this call several times until it determines the exact position for the new item.

If the hwndItem member of the COMPAREITEMSTRUCT structure belongs to a CListBox or CComboBox object, then the CompareItem virtual function of the appropriate class is called. Override CComboBox::CompareItem or CListBox::CompareItem in your derived CListBox or CComboBox class to do the item comparison.


This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.

Header: afxwin.h

Community Additions