Solved

Adding Items to a CListCtrl

Posted on 1998-02-19
4
389 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

20 Experts available now in Live!

Get 1:1 Help Now