Click here to Skip to main content
15,885,366 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 483.6K   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

 
GeneralHelp problemswhen i open the dialog again Pin
Emiliano907-Feb-06 17:55
Emiliano907-Feb-06 17:55 
GeneralRe: Help problemswhen i open the dialog again Pin
nm_zjf6-Dec-06 15:43
nm_zjf6-Dec-06 15:43 
GeneralSelecting a row Pin
Zoltan4-Jan-06 4:33
Zoltan4-Jan-06 4:33 
GeneralRe: Selecting a row Pin
frankpetersen19-May-07 7:21
frankpetersen19-May-07 7:21 
QuestionHelp the control causes a crash Pin
tarink28-Nov-05 12:43
tarink28-Nov-05 12:43 
AnswerRe: Help the control causes a crash Pin
huntkao118625-Jan-18 0:15
huntkao118625-Jan-18 0:15 
GeneralNeed help to use the Sort Iist Control with XP-Style! Pin
bosfan30-May-05 22:41
bosfan30-May-05 22:41 
GeneralRe: Need help to use the Sort Iist Control with XP-Style! Pin
Arcrest22-Jun-05 16:21
Arcrest22-Jun-05 16:21 
OnPaint is called automatically by the MFC framework for a WM_PAINT message
You can invoke Invalidate() method to send WM_PAINT message.
GeneralRe: Need help to use the Sort Iist Control with XP-Style! Pin
mavru20-Nov-08 4:11
mavru20-Nov-08 4:11 
GeneralRe: Need help to use the Sort Iist Control with XP-Style! Pin
bosfan20-Nov-08 5:44
bosfan20-Nov-08 5:44 
QuestionUNICODE? Pin
trandainghia23-May-05 20:59
trandainghia23-May-05 20:59 
AnswerRe: UNICODE? Pin
as_sound_as30-Jan-07 3:22
as_sound_as30-Jan-07 3:22 
Generalslight tightening of compare function selection Pin
dbarndt25-Apr-05 4:13
dbarndt25-Apr-05 4:13 
GeneralRe: slight tightening of compare function selection Pin
RedFraggle5-Mar-06 5:13
RedFraggle5-Mar-06 5:13 
QuestionRe: slight tightening of compare function selection Pin
Rui Frazao8-May-07 6:10
Rui Frazao8-May-07 6:10 
General(void) bullshitting Pin
i_a_z9-Dec-04 1:29
i_a_z9-Dec-04 1:29 
GeneralRe: (void) bull****ting Pin
Jörgen Sigvardsson9-Dec-04 2:42
Jörgen Sigvardsson9-Dec-04 2:42 
Generalredifing headers Pin
RSE Thomas2-Dec-04 22:43
RSE Thomas2-Dec-04 22:43 
GeneralMissing functions Pin
VKostka15-Nov-04 4:17
VKostka15-Nov-04 4:17 
GeneralRe: Missing functions Pin
Daniel Irwin1-Sep-05 2:56
Daniel Irwin1-Sep-05 2:56 
GeneralRe: Missing functions Pin
cristitomi6-Apr-07 4:25
cristitomi6-Apr-07 4:25 
AnswerRe: Missing functions Pin
Coruscant4-Dec-07 11:35
Coruscant4-Dec-07 11:35 
GeneralBug in SetSortArrow Pin
paulg74212-Oct-04 13:49
paulg74212-Oct-04 13:49 
Generalsolution for a problem which is not even a problem to begin with.. Pin
Yawar Maajed11-Jul-04 7:17
Yawar Maajed11-Jul-04 7:17 
GeneralRe: solution for a problem which is not even a problem to begin with.. Pin
AnthonyJ12-Jul-04 2:01
AnthonyJ12-Jul-04 2:01 

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.