Solved

Adding Items to a CListCtrl

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Expand data scrubbing tool 13 35
Define unique primary key 9 95
bigDiff challenge 17 126
sum67 challenge 35 108
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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