list control sorting

I'm trying to figure out how to sort columns in my list control but all the examples I find show it with Listviews. Surely there is an easier way. Any advice?


  • [color=Blue]
    To help you I need two things:

    1. How do you create your list control? If possible, few lines of code how you do it.

    2. How did you make columns in that control? Also, some code would be nice.[/color]
  • Well, I am experimenting with the LISTHDR code that was included in the MFC examples from vc6.

    Here is the jist:

    [code] int iIcon, iItem, iSubItem, iActualItem;
    LV_ITEM lvitem;
    CString strItem1= _T("ITEM");
    CString strItem2= _T("SUB_ITEM");
    CString strIconDesc[4], strIconShortDesc[4];
    LPTSTR pStrTemp1, pStrTemp2;
    CListHdrApp *pApp;

    pApp = (CListHdrApp *)AfxGetApp();

    // insert two columns (REPORT mode) and modify the new header items
    m_listctrl.InsertColumn(0, strItem1, LVCFMT_LEFT,
    rect.Width() * 1/3, 0);
    m_listctrl.InsertColumn(1, strItem2, LVCFMT_LEFT,
    rect.Width() * 2/3, 1);

    for (iItem = 0; iItem < 20; iItem++) // insert the items and subitems into the list view.
    for (iSubItem = 0; iSubItem < 2; iSubItem++)
    if (iSubItem == 0)
    iIcon = rand() % 4; // choose the icon and legend for the entry

    lvitem.mask = LVIF_TEXT | (iSubItem == 0? LVIF_IMAGE : 0);
    lvitem.iItem = (iSubItem == 0)? iItem : iActualItem;
    lvitem.iSubItem = iSubItem;

    // calculate the main and sub-item strings for the current item
    pStrTemp1= strIconShortDesc[iIcon].GetBuffer(strIconShortDesc[iIcon].GetLength());
    pStrTemp2= strIconDesc[iIcon].GetBuffer(strIconDesc[iIcon].GetLength());
    lvitem.pszText = iSubItem == 0? pStrTemp1 : pStrTemp2;

    lvitem.iImage = iIcon;
    if (iSubItem == 0)
    iActualItem = m_listctrl.InsertItem(&lvitem); // insert new item
    m_listctrl.SetItem(&lvitem); // modify existing item (the sub-item text)

    m_listctrl is a CListCtrl. I'm suspecting that I may need to derive a CListCtrl and plug into the msg loop.
  • [color=Blue]I was confused when you said that all codes on the web mention listviews and you have list control. Actually you have the same listview. If you search GOOGLE you will find a lot of code samples like that one:

  • Thanks but now I'm more confused. I thought listviews were incorporated in the document/view architecture. What's the difference between a listview and listctrl?
  • [color=Blue]You were correct. listview in your case is the view Microsoft came up with to wrap listctrl into CView oject so it can be used in MFC. These are same things, just listctrl is an object you use to access Win32 API for List View Controls described here:
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!