CListCtrl with full row selection and sorting of columns 
Author Message
 CListCtrl with full row selection and sorting of columns

A lot of people ask for full row selection and sorting of columns, I
snapped the code below from a derived CListCtrl class of mine, use it
if you can.

Torben

---------------------------------------------------------------------------

#include <afxcmn.h>

class CMyListCtrl : public CListCtrl
{
public:
        CMyListCtrl();
        virtual ~CMyListCtrl() {};
        virtual void PreSubclassWindow();

        BOOL SortItems(int nColumn = -1, int nOrder = 1);

private:
        #define MAXCOLUMNS 32

        int             m_nSortColumn;
        int             m_pSortOrders[MAXCOLUMNS];

Quote:
};

CMyListCtrl::CMyListCtrl()
{
        m_nSortColumn = -1;
        for (int i=0; i < MAXCOLUMNS; i++) m_pSortOrders[i] = -1;

Quote:
}

void CMyListCtrl::PreSubclassWindow()
{
        ModifyStyle(0, LVS_REPORT);
        SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

Quote:
}

static int CALLBACK CMyListCtrl_CompareFunc(LPARAM nItem1, LPARAM nItem2, LPARAM lParams)
{
        CMyListCtrl* pList = ((CMyListCtrl **) lParams)[0];
        int nSubItem = ((int *) lParams)[1];
        int nOrder = ((int *) lParams)[2];
        int nCompare = lstrcmp(pList->GetItemText(nItem1, nSubItem), pList->GetItemText(nItem2, nSubItem));
        return nOrder * nCompare;

Quote:
}

BOOL CMyListCtrl::SortItems(int nColumn, int nOrder)
{
        // if nColumn < 0 then resort with the last used column and order
        // nOrder > 0 => assending, nOrder < 0 => desending, nOrder = 0 => toggle

        if (nColumn >= 0)
        {
                m_nSortColumn = nColumn;
                m_pSortOrders[m_nSortColumn] = (nOrder != 0) ? nOrder : -m_pSortOrders[m_nSortColumn];
        }
        if (m_nSortColumn < 0) return FALSE;

        // Reset the param entry to point to the new index
        LV_ITEM lvi;
        lvi.mask = LVIF_PARAM;
        lvi.iSubItem = 0;
        for (lvi.iItem = 0; GetItem(&lvi); lvi.iItem++)
        {
                lvi.lParam = lvi.iItem;
                SetItem(&lvi);
        }
        // Set the parameters for the compare function
        long lParams[3] = {(long) this, (long) m_nSortColumn, (long) m_pSortOrders[m_nSortColumn]};
        // Sort the items
        BOOL bSorted = CListCtrl::SortItems(&CMyListCtrl_CompareFunc, (LPARAM) lParams);
        // Ensure that selected item is visible
        int nIndex = GetCurSel();
        if (nIndex >= 0) EnsureVisible(nIndex, FALSE);

        return bSorted;

Quote:
}

class CMyDialog : public CDialog
{
public:
        CMyListCtrl     m_cMyListCtrl;

protected:
        virtual void DoDataExchange(CDataExchange* pDX);
        afx_msg void OnColumnclickMylistctrl(NMHDR* pNMHDR, LRESULT* pResult);
        DECLARE_MESSAGE_MAP()

Quote:
}

void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
        CCenterDialog::DoDataExchange(pDX);
        DDX_Control(pDX, IDC_MYLISTCTRL, m_cMyListCtrl);

Quote:
}

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
        ON_NOTIFY(LVN_COLUMNCLICK, IDC_MYLISTCTRL, OnColumnclickMylistctrl)
END_MESSAGE_MAP()

void CMyDialog::OnColumnclickMylistctrl(NMHDR* pNMHDR, LRESULT* pResult)
{
        NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

        m_cMyListCtrl.SortItems(pNMListView->iSubItem, 0);

        *pResult = 0;

Quote:
}



Mon, 17 Jul 2000 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. CListCtrl:: full row selection

2. CListCtrl: Sorting by Column: Sort function failure

3. full row selection on a list control

4. How to get full row selection in List views

5. Simple code for ListView full row selection

6. Full row select in CListCtrl

7. CListCtrl full row select

8. CListCtrl full row select

9. CListCtrl with full row select

10. CListCtrl with full row select

11. CListCtrl - Selecting Full Row Inside A Form View

12. CListCtrl - How To Display Full Row Select?

 

 
Powered by phpBB® Forum Software