CSortListBox API
Copyright © 2026 by David Wincelberg

Overview

The parameters of the CSortListBox constructor relate to the Strategy and Flyweight patterns. You can specify which order object to use or don't pass parameters in order to use the default one. You can also specify whether this class should handle deleting the order object.

Here is a simple way of connecting a list box to this class and changing the string order. m_listBox is a CSortListBox object. The default order is alphabetic ordering.

m_listBox.SubclassDlgItem (IDC_SORT_LISTBOX, this);
m_listBox.SetStringOrder (new CStringSANOrder());

Window Styles

Recommended appearance styles Border, Horizontal Scroll, No Integral Height, Vertical Scroll
Required behavior styles Owner Draw: No
Recommended behavior styles Sort, Visible

CSortListBox Functions

CSortListBox (CListBoxOrder* pListBoxOrder = nullptr, bool pHandleDeleteListBoxOrder = true);

constructor

pListBoxOrder pointer to a list-box order object
pHandleDeleteListBoxOrder specifies whether to delete this list-box order object

CSortListBox (CStringOrder* pStringOrder, bool bHandleDeleteStringOrder = true);

constructor

pStringOrder pointer to a string order object
bHandleDeleteStringOrder specifies whether to delete this string order object

void SetSortOrder (CListBoxOrder* pListBoxOrder, bool pHandleDeleteListBoxOrder = true);

Sets the list-box order to the specified value.

pListBoxOrder pointer to list-box order object
pHandleDeleteListBoxOrder specifies whether to delete this list-box order object

void SetStringOrder (CStringOrder* pStringOrder, bool bHandleDeleteStringOrder = true);

Sets the string order to the specified value.

pStringOrder pointer to string order object
bHandleDeleteStringOrder specifies whether to delete this string order object

int SetItemDataObjPtr (int nIndex, CListBoxData* pListBoxData);

Stores user data as a pointer to a CListBoxData object. Deletes the old data object if any.

nIndex index into the list box
pListBoxData pointer to a data object

CListBoxData* GetItemDataObjPtr (int nIndex) const;

Retrieves user data as a pointer to a CListBoxData object.

nIndex index into the list box

The following are the same as for CListBox with an optional parameter for specifying a list-box data item.

int AddString (LPCTSTR lpszItem, CListBoxData* pListBoxData = nullptr);
int InsertString (int nIndex, LPCTSTR lpszItem, CListBoxData* pListBoxData = nullptr);

The following are the same as for CListBox.

int DeleteString (UINT nIndex);
void ResetContent();
int SetItemData (int nIndex, DWORD dwItemData);
DWORD GetItemData (int nIndex) const;
int SetItemDataPtr (int nIndex, void* pData);
void* GetItemDataPtr (int nIndex) const;

CListBoxData Functions

CListBoxData();

constructor

void SetExtent (int nExtent);

Stores the horizontal extent.

nIndex index into the list box

int GetExtent();

Retrieves the horizontal extent.

void SetInserted();

Marks an item as inserted.

bool GetAdded();

Returns true if the item was added.

void SetItemData (DWORD dwItemData);

Stores the item's data.

dwItemData associated data to store

DWORD GetItemData();

Retrieves the item's data.

CListBoxOrder Functions

CListBoxOrder (CStringOrder* pStringOrder = nullptr, bool bHandleDelete = true);

constructor

pStringOrder pointer to a string order object
bHandleDelete specifies whether to delete this string order object

void SetStringOrder (CStringOrder* pStringOrder, bool bHandleDelete = true);

Sets the string order to the specified value.

pStringOrder pointer to a string order object
bHandleDelete specifies whether to delete this string order object

bool ValidStringOrder();

Returns true if the string order object is not null.

CStringOrder* GetStringOrder();

Returns the string order object.

virtual int Compare (CString& strItem1, CListBoxData* pData1, CString& strItem2, CListBoxData* pData2);

Returns an integer that specifies order of the strings or the corresponding data items. An example of comparison based on data is date entered.

strItem1 reference to the first string
pData1 pointer to the first data item
strItem2 reference to the second string
pData2 pointer to the second data item

CExtentCalcs Functions

These functions may be called for controlling extent calculations.

void DeferExtentCalc (bool bDefer);

Defers or may call updating the maximum extent.

bDefer true for defer; false for updating maximum extent

void RecalcHorizontalExtent();

Recalculates and sets the list-box's horizontal extent.

void FontsChanged();

Recalculates extents if the font resources have changed. The top-level window receives the OnFontChange message. It should pass it to list-box controls with extent calculations.


Last updated: 29-Oct-2026