Solved

Adding Items to a CListCtrl

Posted on 1998-02-19
4
384 Views
Last Modified: 2013-11-20
Hi,
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);
  m_Messages.CreateEx(WS_EX_CLIENTEDGE,"LISTBOX",NULL,
      WS_CHILD|WS_VISIBLE|LBS_STANDARD|LVS_SINGLESEL,
      Rect.left,Rect.top, Rect.right, Rect.bottom,         m_hWnd,(HMENU)IDC_MESSAGES);
      
   CImageList imglLarge;
   imglLarge.Create(IDR_LARGEDOC,32,1,RGB(255,0,255));
   CImageList imglSmall;
   imglSmall.Create(IDR_SMALLDOC,16,1,RGB(255,0,255));

   m_Messages.SetImageList(&imglLarge,LVSIL_NORMAL);
   m_Messages.SetImageList(&imglSmall,LVSIL_SMALL);

   m_Messages.InsertColumn(0,"From",LVCFMT_LEFT,100);
   m_Messages.InsertColumn(1,"Subject",LVCFMT_CENTER,100);
   m_Messages.InsertColumn(2,"Date",LVCFMT_RIGHT,100);
   
   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!");
   e->Delete();
   return;
   }

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

LV_ITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
lvi.iItem = nIndex;
lvi.iSubItem = 0;
lvi.iImage = 0;
lvi.pszText = LPSTR_TEXTCALLBACK;
lvi.lParam = (LPARAM) pItem;

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

 return;
}


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,
Eoin.
0
Comment
Question by:estronge
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
Comment Utility
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.

0
 

Author Comment

by:estronge
Comment Utility
Hi again,
        I added the following code to my program, but it never
executes:

// In the message map
ON_NOTIFY(LVN_GETDISPINFO,IDC_MESSAGES,OnGetDispInfo)
// I also tried ON_NOTIFY(TVN_GETDISPINFO, IDC_MESSAGES, // OnGetDispInfo)

// 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);
         break;

        case 1: // Subject:
         :lstrcpy(plvdi->item.pszText,(LPCTSTR)pItem->szSubject);
          break;                                                      

        case 2: // Date
         ::lstrcpy (plvdi->item.pszText, (LPCTSTR)pItem->szDate);
         break;
        } // 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.

Thanks,
Eoin.

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 150 total points
Comment Utility
Derive a class from CListCtrl which includes the handler for OnGetDispInfo - needs to be
ON_NOTIFY_REFLECT(LVN_GETDISPINFO, OnGetdispinfo)
...
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.




0
 

Author Comment

by:estronge
Comment Utility
Hi,
  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,
Eoin.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now