?
Solved

Adding Items to a CListCtrl

Posted on 1998-02-19
4
Medium Priority
?
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

800 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