Click here to Skip to main content
Click here to Skip to main content

Sort List Control

By , 6 Sep 2001
 

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

About the Author

Mark Jackson
Web Developer
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4groupjeffywin22 Jun '11 - 16:53 
GeneralLicencing terms and conditions in a commercial productmemberR. Choudhury27 Mar '11 - 20:56 
GeneralLicensing for commercial applicationmemberwill626219 Oct '10 - 5:09 
Generaljump to linememberaddict8527 Jan '09 - 4:10 
GeneralWasteful approachmemberPanic2k33 Dec '08 - 16:28 
GeneralWant to add integermembermailtochandra2000@yahoo.com12 Nov '08 - 0:40 
Questionchange headings?membergeneral_era1 Oct '08 - 22:21 
AnswerRe: change headings?memberEPausU17 Jul '09 - 0:31 
GeneralQuestion about License...memberBK CHA7 Sep '08 - 17:05 
GeneralNeed help inserting integer items into a list controlmemberdromichetes3 Sep '08 - 21:33 
QuestionCListCtrl with virtual list and Images?membertermal13 Aug '08 - 3:21 
Questionwhy no selection?membergeneral_era5 May '08 - 23:13 
QuestionHow to process List Item click event?memberzotiger22228 Nov '07 - 21:15 
GeneralColumn separators are not displayingmembera_snm11 Oct '07 - 2:41 
QuestionUsing Virtual lists?membertermal5 Jun '07 - 22:34 
GeneralProblem with SetFont()membertermal18 Apr '07 - 3:01 
QuestionChange rowheight in CSortListCtrl!?memberbreak;17 Jan '07 - 2:59 
Generalderive a CCtrlView from a CSortListCtrlmembermimosa30 Apr '06 - 19:40 
GeneralHelp problemswhen i open the dialog againmemberEmiliano907 Feb '06 - 17:55 
GeneralRe: Help problemswhen i open the dialog againmembernm_zjf6 Dec '06 - 15:43 
GeneralSelecting a rowmemberZoltan4 Jan '06 - 4:33 
GeneralRe: Selecting a rowmemberfrankpetersen19 May '07 - 7:21 
QuestionHelp the control causes a crashmembertarink28 Nov '05 - 12:43 
GeneralNeed help to use the Sort Iist Control with XP-Style!membermirso6730 May '05 - 22:41 
GeneralRe: Need help to use the Sort Iist Control with XP-Style!membereXRange22 Jun '05 - 16:21 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 7 Sep 2001
Article Copyright 2001 by Mark Jackson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid