Click here to Skip to main content
15,890,557 members
Articles / Desktop Programming / MFC
Article

Sort List Control

Rate me:
Please Sign up or sign in to vote.
4.45/5 (64 votes)
6 Sep 20012 min read 484.8K   5.8K   118   139
A list control with inbuilt sorting, and an easier way to add columns and rows

Sample Image - screenshot.jpg

Introduction

This adds sorting to MFC's list control class. Sorting is automatically taken care of: it will sort text, dates and numbers in ascending or descending order, and show an arrow in the appropriate direction in the heading for the sorted column. It also adds some other things that make life easier when using list controls - it is much easier to set the columns and add rows, and it can load and save the column widths.

How to use it

Look at the example to see how it is used. You need to add the files SortListCtrl.cpp/h and SortHeaderCtrl.cpp/h to your project, then associate a CSortListCtrl variable with your list control (you can do this with ClassWizard).

In the OnInitDialog member function of your dialog class you set the columns and their initial widths by calling the list control's SetHeadings function, it takes a string, or a string ID in the string table, which defines the column headings and their widths, 

e.g.

m_ctlList.SetHeadings( _T("Name,120;Date of Birth,90;Score,50") );

Adding rows is very easy, you call the list control's AddItem function with the same number of strings as the number of columns you added, 

e.g.

m_ctlList.AddItem( _T("Mark Jackson"), _T("09/08/1974"), _T("100") );

To remember the columns' widths call the list control's LoadColumnInfo function after setting the headings, add a handler for your dialog's WM_DESTROY message and in there call SaveColumnInfo.

Implementation

It was a REAL pain to do, your callback function gets the item data for the two rows to compare, but what use is that, you need the text to compare! This control stores the text for the columns in the item data, so the compare function can get at it, it also allows users of the control to use the item data as usual.

Credits

The code for drawing the arrow in the header control was written by Zafir Anjum

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: solution for a problem which is not even a problem to begin with.. Pin
sps-itsec469-Mar-06 3:46
sps-itsec469-Mar-06 3:46 
Questionhow to get the current position? Pin
Desvariovsk1-Jun-04 0:15
Desvariovsk1-Jun-04 0:15 
AnswerRe: how to get the current position? Pin
navi200024-Aug-04 2:45
navi200024-Aug-04 2:45 
Generalvery good Pin
Alex Piko13-Apr-04 12:04
Alex Piko13-Apr-04 12:04 
GeneralBug!!!Memory leak!!! Pin
Johnson Chen6-Apr-04 16:55
Johnson Chen6-Apr-04 16:55 
GeneralUnable to create CSortListCtrl object in View Pin
Member 72960631-Dec-03 7:40
Member 72960631-Dec-03 7:40 
QuestionBug!! Can anyone help? Pin
Ahmed Al-Qassem10-Oct-03 11:36
Ahmed Al-Qassem10-Oct-03 11:36 
Questionselect an item/a row? Pin
webwesen6-Oct-03 12:11
webwesen6-Oct-03 12:11 
AnswerRe: select an item/a row? Pin
Member 63412619-Oct-03 19:00
Member 63412619-Oct-03 19:00 
GeneralRe: select an item/a row? Pin
webwesen20-Oct-03 4:32
webwesen20-Oct-03 4:32 
QuestionHow to Get Item Text? Pin
Mikey_E4-Sep-03 21:58
professionalMikey_E4-Sep-03 21:58 
Generalrough code to add arrow to xp-styled ctrls Pin
mightyCoCo20-Aug-03 0:26
mightyCoCo20-Aug-03 0:26 
GeneralRe: rough code to add arrow to xp-styled ctrls Pin
Yin Gang6-Jun-05 23:00
Yin Gang6-Jun-05 23:00 
Generalsome suggestions Pin
sea_soul23-Jul-03 15:39
sea_soul23-Jul-03 15:39 
GeneralError in dynamic creation Pin
Member 37503618-May-03 5:40
Member 37503618-May-03 5:40 
GeneralRe: Error in dynamic creation Pin
fieldart28-Sep-03 16:57
fieldart28-Sep-03 16:57 
GeneralRe: Error in dynamic creation Pin
Manfred Staiger13-Oct-03 3:38
Manfred Staiger13-Oct-03 3:38 
GeneralRe: Error in dynamic creation Pin
Fzz1-Nov-03 1:56
Fzz1-Nov-03 1:56 
GeneralRe: Error in dynamic creation Pin
Manfred Staiger2-Nov-03 21:57
Manfred Staiger2-Nov-03 21:57 
GeneralRe: Error in dynamic creation Pin
Fzz6-Nov-03 4:52
Fzz6-Nov-03 4:52 
GeneralRe: Error in dynamic creation Pin
Manfred Staiger6-Nov-03 4:59
Manfred Staiger6-Nov-03 4:59 
GeneralRe: Error in dynamic creation Pin
t2di4u19-Mar-04 15:13
t2di4u19-Mar-04 15:13 
GeneralRe: Error in dynamic creation Pin
gianct21-Nov-03 0:05
gianct21-Nov-03 0:05 
GeneralRe: Error in dynamic creation Pin
Member 72960630-Dec-03 9:19
Member 72960630-Dec-03 9:19 
GeneralRe: Error in dynamic creation Pin
Adam Bialowas4-Feb-05 6:24
Adam Bialowas4-Feb-05 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.