Adding Items to a CListCtrl

I have a listbox that seems to have a mind of it's own.
It's declared as part of my main window derived from CFrameWnd, CListCtrl m_Messages;

I create it as follows:
  Rect.SetRect(Cx * 33, Cy * 5, Cx * 52, Cy * 18);
      Rect.left,, Rect.right, Rect.bottom,         m_hWnd,(HMENU)IDC_MESSAGES);
   CImageList imglLarge;
   CImageList imglSmall;


   m_Messages.ModifyStyle ( LVS_TYPEMASK, LVS_REPORT );

And I try to add to it using the following fxn:

void CMainWindow::AddItem( int nIndex, CString strFrom,                   CString strSubject, CString strDate )
 ITEMINFO* pItem; // User defined basic struct, 3 CStrings
 try {
        pItem = new ITEMINFO;
catch (CMemoryException* e) {
   MessageBox("Guess who's having problems with memory!");

pItem->szFrom = strFrom;         // Fill the struct
pItem->szSubject = strSubject;   // 3 CString objects
pItem->szDate = strDate;

LV_ITEM lvi;
lvi.iItem = nIndex;
lvi.iSubItem = 0;
lvi.iImage = 0;
lvi.lParam = (LPARAM) pItem;

if (m_Messages.InsertItem( &lvi ) == -1)
    MessageBox("Not inserted"); // This has never exec'd.


The problem is when I try to add these items, it doesn't
Messagebox the error if not added, but yet doesn't add it
to the list box. The listbox remains empty, without column
headers either.

Can anybody help?

Thanks in advance,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tommy HuiEngineerCommented:
If you tell the list control to use the callback mechanism for the item, then your control must also handle the TVN_GETDISPINFO message. The control sends this message when it needs to display the information.

estrongeAuthor Commented:
Hi again,
        I added the following code to my program, but it never

// In the message map

// One of which should run this fxn
void CMainWindow::OnGetDispInfo( NMHDR* pnmh, LRESULT* pResult)
    MessageBox("Getting disp info"); //This has never appeared
    CString string;
    LV_DISPINFO* plvdi = (LV_DISPINFO*) pnmh;

    if (plvdi->item.mask & LVIF_TEXT) {
        ITEMINFO* pItem = (ITEMINFO*) plvdi->item.lParam;

        switch (plvdi->item.iSubItem) {

        case 0: // From:
         ::lstrcpy (plvdi->item.pszText,(LPCTSTR)pItem->szFrom);

        case 1: // Subject:

        case 2: // Date
         ::lstrcpy (plvdi->item.pszText, (LPCTSTR)pItem->szDate);
        } // End switch
    } // End if
} // End fxn.

But this function never runs, as the MessageBox at the start doesn't appear nor does anything into the listbox. It's running
on win95.


Derive a class from CListCtrl which includes the handler for OnGetDispInfo - needs to be
void CMyListCtrl::OnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult) {

You might also like to put some of your other methods related to this list control into the class, rather than directly in the mainframe class.  It's problably a bit nicer doing that from an OO point of view.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
estrongeAuthor Commented:
  Thanks for your answer. I tried that, and I added the OnGetDispInfo method, but it never executes. I have the message map entry ok, and I put a MessageBox call in the OnGetDispInfo fxn, but it never shows. Is there any reason why it may not send a LVN_GETDISPINFO message?

Thanks again,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.