Howdy, Stranger!

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

Categories

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?

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [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]
  • kennerdlkennerdl Member Posts: 3
    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.GetWindowRect(&rect);
    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
    else
    m_listctrl.SetItem(&lvitem); // modify existing item (the sub-item text)
    }
    }[/code]


    m_listctrl is a CListCtrl. I'm suspecting that I may need to derive a CListCtrl and plug into the msg loop.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [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:

    [link=http://www.developer.com/net/cplus/article.php/602611]http://www.developer.com/net/cplus/article.php/602611[/link]
    [/color]
  • kennerdlkennerdl Member Posts: 3
    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?
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [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:
    [link=http://msdn.microsoft.com/en-us/library/bb774737(VS.85).aspx]http://msdn.microsoft.com/en-us/library/bb774737(VS.85).aspx[/link]
    [/color]
Sign In or Register to comment.