ClistCtrl More help needed
-
I have a CListCtrl with 3 columns I am using this function to know when someone clicks on the column button void OnColumnclickList2(NMHDR* pNMHDR, LRESULT* pResult) the problem is it is sorting the list according to the first column no matter what column button i press what sshould i do to make it sort according to the column button i press? In other word how can i know which column button has been pressed? /\|-||\/|/\|)
-
I have a CListCtrl with 3 columns I am using this function to know when someone clicks on the column button void OnColumnclickList2(NMHDR* pNMHDR, LRESULT* pResult) the problem is it is sorting the list according to the first column no matter what column button i press what sshould i do to make it sort according to the column button i press? In other word how can i know which column button has been pressed? /\|-||\/|/\|)
Take a look at CListCtrl::SortItems[^] If you pass both the list control pointer and a column index in some struct as the dwData parameter to SortItems, your compare function can obtain the text for the appropriate column and use it for the comparison.
struct SORT_DATA
{
CListCtrl* pList;
int nColumn;
}
static int CALLBACK
MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
SORT_DATA* pData = (SORT_DATA*) lParamSort;
CListCtrl* pListCtrl = pData->pList;
int nColumn = pData->nColumn
CString strItem1 = pListCtrl->GetItemText(lParam1, nColumn);
CString strItem2 = pListCtrl->GetItemText(lParam2, nColumn);
return strcmp(strItem1, strItem2);
}
void CListCtrl_SortItems(CListCtrl* pList, int nColumn)
{
SORT_DATA data = {pList, nColumn};
// Sort the list view items using my callback procedure.
pList->SortItems(&MyCompareProc, (LPARAM) &data);
}You could easily extend this to allow for sorting order (ascending/descending), sort on multiple columns, etc. Hope that helps, -- jlr http://jlamas.blogspot.com/[^]
-
I have a CListCtrl with 3 columns I am using this function to know when someone clicks on the column button void OnColumnclickList2(NMHDR* pNMHDR, LRESULT* pResult) the problem is it is sorting the list according to the first column no matter what column button i press what sshould i do to make it sort according to the column button i press? In other word how can i know which column button has been pressed? /\|-||\/|/\|)
The way I've handled this in the past was something like:
BEGIN_MESSAGE_MAP(CMyView, CListView)
//{{AFX_MSG_MAP(CMyView)
ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclick)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()int CALLBACK CMyView::CompareFunc( LPARAM lParam1, LPARAM lParam2, LPARAM )
{
int nReturn = 0;
CInfo *pInfo1,
*pInfo2,
*pTemp;pInfo1 = (CInfo \*) lParam1; pInfo2 = (CInfo \*) lParam2; if (true == m\_bAscending) { pTemp = pInfo1; pInfo1 = pInfo2; pInfo2 = pTemp; } switch (m\_nSortColumn) { // name case 0: nReturn = pInfo1->m\_strName.Compare(pInfo2->m\_strName); break; // USAG # case 1: nReturn = pInfo1->m\_lUSANumber - pInfo2->m\_lUSANumber; break; // Age case 2: if (pInfo1->m\_timeBirthdate < pInfo2->m\_timeBirthdate) nReturn = -1; else nReturn = 1; break; } return (nReturn);
}
void CMyView::OnItemclick(NMHDR* pNMHDR, LRESULT* pResult)
{
HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
CListCtrl &ctrlList = GetListCtrl();m\_bAscending = ! m\_bAscending; **m\_nSortColumn = phdn->iItem;** ctrlList.SortItems(CompareFunc, 0); \*pResult = 0;
}
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
Take a look at CListCtrl::SortItems[^] If you pass both the list control pointer and a column index in some struct as the dwData parameter to SortItems, your compare function can obtain the text for the appropriate column and use it for the comparison.
struct SORT_DATA
{
CListCtrl* pList;
int nColumn;
}
static int CALLBACK
MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
SORT_DATA* pData = (SORT_DATA*) lParamSort;
CListCtrl* pListCtrl = pData->pList;
int nColumn = pData->nColumn
CString strItem1 = pListCtrl->GetItemText(lParam1, nColumn);
CString strItem2 = pListCtrl->GetItemText(lParam2, nColumn);
return strcmp(strItem1, strItem2);
}
void CListCtrl_SortItems(CListCtrl* pList, int nColumn)
{
SORT_DATA data = {pList, nColumn};
// Sort the list view items using my callback procedure.
pList->SortItems(&MyCompareProc, (LPARAM) &data);
}You could easily extend this to allow for sorting order (ascending/descending), sort on multiple columns, etc. Hope that helps, -- jlr http://jlamas.blogspot.com/[^]
-
The way I've handled this in the past was something like:
BEGIN_MESSAGE_MAP(CMyView, CListView)
//{{AFX_MSG_MAP(CMyView)
ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclick)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()int CALLBACK CMyView::CompareFunc( LPARAM lParam1, LPARAM lParam2, LPARAM )
{
int nReturn = 0;
CInfo *pInfo1,
*pInfo2,
*pTemp;pInfo1 = (CInfo \*) lParam1; pInfo2 = (CInfo \*) lParam2; if (true == m\_bAscending) { pTemp = pInfo1; pInfo1 = pInfo2; pInfo2 = pTemp; } switch (m\_nSortColumn) { // name case 0: nReturn = pInfo1->m\_strName.Compare(pInfo2->m\_strName); break; // USAG # case 1: nReturn = pInfo1->m\_lUSANumber - pInfo2->m\_lUSANumber; break; // Age case 2: if (pInfo1->m\_timeBirthdate < pInfo2->m\_timeBirthdate) nReturn = -1; else nReturn = 1; break; } return (nReturn);
}
void CMyView::OnItemclick(NMHDR* pNMHDR, LRESULT* pResult)
{
HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
CListCtrl &ctrlList = GetListCtrl();m\_bAscending = ! m\_bAscending; **m\_nSortColumn = phdn->iItem;** ctrlList.SortItems(CompareFunc, 0); \*pResult = 0;
}
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
Halawlaws wrote: refer to this link to know why this sortitem() doesn't work anyway Hmm.. yep. You are right. But it's easy to fix, isn't it? You just need to set lParamData (while inserting items) with a pointer to the object from which you obtained the strings, and modify the compare function accordingly. -- jlr http://jlamas.blogspot.com/[^]
-
Halawlaws wrote: refer to this link to know why this sortitem() doesn't work anyway Hmm.. yep. You are right. But it's easy to fix, isn't it? You just need to set lParamData (while inserting items) with a pointer to the object from which you obtained the strings, and modify the compare function accordingly. -- jlr http://jlamas.blogspot.com/[^]