Malek103197
asked on
CListCtrl::SortItems?
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
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::OnColumncli ckList1(NM HDR* 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()->SetModified Flag() ; // <-- 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) ;
} ;
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::OnColumncli
{
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()->SetModified
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
{
// 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(
CString str2 = pView->m_list.GetItemText(
//
// 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) ;
} ;
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.
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.
ASKER
Sorry, but I need more details (like the code) to understand the answer.
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...
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
In hendler use CListCtrl::SortItems to sort