Solved

CListCtrl::SortItems?

Posted on 1998-11-20
8
751 Views
Last Modified: 2013-11-20
I'm unable to implement a column sort in my CListCtrl by pushing the column header button on the ListCtrl box. Any suggestions? I've tried the Help menu but with no luck. I am using VC++ 5.0
0
Comment
Question by:Malek103197
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:cyrilbdt
ID: 1325060
derive your owne class from CListCtrl. override LVN_COLUMNCLICK.
In hendler use CListCtrl::SortItems to sort

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1325061
cyrilbdt posted an answer before I had a change to submit my long answer...damn!

He missed out how to use the CompareFunc so his answer is incomplete.

Hopefully this tells you how...

This is for a CListCtrl on a view, but the idea is the same for derived classes of CListCtrl (replace m_list with this->)

1. Add m_nSortColumn (int) and m_bSortReverse (BOOL) flags to your view class

2. Add a click on column header (LVN_COLUMNCLICK)

void CMailGuruView::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
      NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

      int nNewHeader = pNMListView->iSubItem ;

      if ( nNewHeader == m_nSortColumn )
      {
            m_bSortReverse = !m_bSortReverse ;
      } else
      {
            m_nSortColumn = nNewHeader ;
            m_bSortReverse = FALSE ;
      }

      GetDocument()->SetModifiedFlag() ; // <-- remove this if not a view
      Resort() ;
      
      *pResult = 0;
}


3. Add a Resort function, which triggers the sort

void CMailGuruView::Resort()
{
      CWaitCursor oWait ;
      ASSERT_VALID(this) ;

      int nCount = m_list.GetItemCount() ;
      
      if ( nCount > 0 )
      {
            for ( int ii = 0 ; ii < nCount ; ii++ )
            {
                  m_list.SetItemData( ii, ii ) ;
            }

            m_list.SortItems( CompareFunc, (DWORD)this ) ;
      }
}

4. Add Compare Function, make sure that the CompareFunc is declared as a static function in the file's header (.h)

// in .cpp

int CALLBACK CMailGuruView::CompareFunc(LPARAM lParam1, LPARAM lParam2,     LPARAM lParamSort)
{
             // change pView to m_pThis if derived from CListCtrl
            // change CMailGuruView * into CMyListCtrl if derived from CListCtrl
      CMailGuruView * pView = (CMailGuruView *)lParamSort ;
      ASSERT_VALID(pView) ;

      int nCmp = 0 ;

      //
      // Get Strings
      //
      CString str1 = pView->m_list.GetItemText( lParam1, 0 ) ;
      CString str2 = pView->m_list.GetItemText( lParam2, 0 ) ;

      //
      // Compare fields
      //

      if ( str1 < str2 )
      {
            nCmp = -1 ;
      }
      
      else if ( str2 > str1 )
      {
            nCmp = 1 ;
      }


      //
      // Return result
      //

      if ( pView->m_bSortReverse )
      {
            nCmp = 0 - nCmp ;
      }

      return nCmp ;
}


// in .h
class __whatever__
{
 // etc

public:
static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2,     LPARAM lParamSort) ;

} ;


0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1325062
Oh in step1

Make sure the m_nSortColumn is initialized (say in your constructor) to the initial sort column, and m_bSortReverse to FALSE.

Also the reason for writing the Resort function, is you can later add stuff like drawing the arrow's in column headers here, plus you can call this whenever the list is changed.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Malek103197
ID: 1325063
Sorry, but I need more details (like the code) to understand the answer.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1325064
Malek - didn't you understand my comment/answer ?  Which bit is not clear.  The code is mostly cut'n'paste'd from an actual program...
0
 

Author Comment

by:Malek103197
ID: 1325065
For answer2000:

My comment was intended for crydblt, whose answer I rejected due to lack of code. Your (answer200) was acceptable, but I don't know how to give you the points. I guess I can't since yours is a comment and not an answer.
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1325066
OK Malek

The normal procedure is :-
1. Reject answers you're not happy with (you did)
2. Ask the expert who gives an answer you're happy with in a comment, to give a dummy answer.  Then grade this based on his/her previous comments.  

Step #2 is necessary because Ex-Ex only allows one expert to post an answer at a time.



0
 

Author Comment

by:Malek103197
ID: 1325067
Thanks for your answer answer2000. How on earth do you find the time to run a successful computer business and answer all these questions on so many different topics? A Rhetorical question if you may.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iSeries DB2 Query 2 95
NotAlone Challenge 20 83
Execute multiple curl cmds with sleep and send output to file 10 97
wordmultiple challenge 12 131
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question