Solved

Adding Items to a CListCtrl

Posted on 1998-02-19
4
391 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
ID: 1316412
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
ID: 1316413
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
ID: 1316414
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
ID: 1316415
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

770 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