CListCtrl - SortItems



  • In der MSDN ist ja so ein tolles Beispiel beschrieben:

    // Sort the item in reverse alphabetical order.
    static int CALLBACK 
    MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
       // lParamSort contains a pointer to the list view control.
       // The lParam of an item is just its index.
       CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
       CString    strItem1 = pListCtrl->GetItemText(lParam1, 0);
       CString    strItem2 = pListCtrl->GetItemText(lParam2, 0);
    
       return strcmp(strItem2, strItem1);
    }
    
    void snip_CListCtrl_SortItems()
    {
       // The pointer to my list view control.
       extern CListCtrl* pmyListCtrl;
    
       // Sort the list view items using my callback procedure.
       pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
    }
    

    Leider ist bei mir das Problem, dass lParam1 und lParam2 immer dieselben Indizes sind, also nicht wirklich was verglichen wird.
    Und demensprechend wird nichts sortiert.

    Liegt es vielleicht daran, dass versucht wird die Liste zu sortieren,
    aber das erste Vergleichsresultat schon in der Liste einbezogen wird und der nächste Sortiervorgang (Element2 < Element3) schon auf der aktualisierten Liste arbeitet?
    Das Problem ergibt sich nämlich auch beim Löschen von Items: Sobald ich das erste selektierte gelöscht habe, liefert mir die next-Funktion noch Indizes der alten Liste (also vor dem ersten Löschvorgang).



  • Hat denn hier noch niemand ein ListControl im Reportmodus nach einer bestimmtem Spalte sortiert?



  • Nachdem ich mich nun selbst schlau gemacht habe, hab ich folgende Erkenntnisse gewonnen. (von diversen Sites)

    When my sorting callback function is called, why are lParam1 and lParam2 always 0?

    Some versions of the documentation on CListCtrl::SortItems and LVM_SORTITEMS seem to imply that the values of lParam1 and lParam2 are the indices of the items to be sorted. This is not the case. They are the 32 bit values that can be assigned to each item via CListCtrl::SetItemData or via the lParam member of the LVITEM struct. If you have not assigned any data to these variables, then they will be zero when passed to your sort function.

    How can I sort the control when the user clicks on a column header?

    To capture the click event: Sorting the list when user clicks on column header
    To draw a sort arrow on the header: Indicating sort order in header control

    Wäre ein Thema für die FAQ.

    [ Dieser Beitrag wurde am 22.01.2002 um 21:22 Uhr von thomas80d editiert. ]




Anmelden zum Antworten