Thread: CListCtrl::SetItem is failing

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    CListCtrl::SetItem is failing

    I have a CListCtrl in report view. When I try
    Code:
    int index = prefsD->m_CcontactList.InsertItem(0,entry->name);
    prefsD->m_CcontactList.SetItem(index, 1, LVIF_TEXT, entry->real_name, 0, 0, 0, NULL);
    prefsD->m_CcontactList.SetItem(index, 2, LVIF_TEXT, entry->medium, 0, 0, 0, NULL);
    The item is inserted correctly by the first line, but both calls to SetItem fail. However if I change the iSubItem (2nd parameter) to 0, they work just fine (but in the wrong column obviously)

    I thought perhaps I had the columns set up incorrectly, but I do not believe this is the case:
    Code:
    m_CcontactList.InsertColumn(0, "Name", LVCFMT_LEFT, 75);
    m_CcontactList.InsertColumn(1, "Real Name", LVCFMT_LEFT, mwidth);
    m_CcontactList.InsertColumn(2, "Medium", LVCFMT_LEFT, mwidth);
    SetItemText does not seem to work either (same results as above)

    I am on WinXP Pro, with VC++ 6.0 and in this project using MFC. Can anyone see my mistake or offer any suggestions?

    Thanks,
    Chris

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Your guess was correct - you need to specify a subItem index for your columns...
    Code:
    m_CcontactList.InsertColumn(0, "Name", LVCFMT_LEFT, 75, 0 /*subItem index!*/);
    m_CcontactList.InsertColumn(1, "Real Name", LVCFMT_LEFT, mwidth, 1 /*subItem index!*/);
    m_CcontactList.InsertColumn(2, "Medium", LVCFMT_LEFT, mwidth, 2 /*subItem index!*/);
    Then this will work:
    Code:
    int index = prefsD->m_CcontactList.InsertItem(0, entry->name);
    //index will be 0 (first param) on success and -1 on failure
    prefsD->m_CcontactList.SetItemText(index, 1 /*subItem index!*/, entry->real_name);
    prefsD->m_CcontactList.SetItemText(index, 2 /*subItem index!*/, entry->medium);
    That should do it.
    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    hmm... That didn't do it. And oddly enough, in the other CListCtrl I have (that does work), I didn't need to specify that subitem index when I created the columns. Thanks anyway

    <edit> In fact when I change the list name in the SetItem call to the other one, That I know works, it still does the same thing. So the problem must be in the SetItem call, right? maybe?
    </edit>
    Last edited by skiingwiz; 03-10-2003 at 03:51 PM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, the API usage is correct so I would start looking elsewhere, like the list resource and CListCtrl creation.
    Take a 5 min break, it might be something embarrassingly obvious when you get back

    Also, check error codes on all calls (make sure columns are actually there).

    gg

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    groan... Thanks for the suggestion of the 5 minute break... this one was embarrassingly obvoius. In my OnInitDialog I was calling the function to fill the CListCtrl before I was creating the columns.

    Thanks for all your help. You have no idea how long I have been staring at this (hours) In the future I'll try the 5 minute break before I post my stupidity here.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    Red face

    Quote Originally Posted by skiingwiz View Post
    groan... Thanks for the suggestion of the 5 minute break... this one was embarrassingly obvoius. In my OnInitDialog I was calling the function to fill the CListCtrl before I was creating the columns.

    Thanks for all your help. You have no idea how long I have been staring at this (hours) In the future I'll try the 5 minute break before I post my stupidity here.
    Code:
    	LVITEM lvi;
    	ZeroMemory(&lvi, sizeof(LVITEM));
    	CString strItem;
    	for (int i = 1; i <= 24; i++)
    	{
    		lvi.mask = LVIF_TEXT;
    		strItem.Format(_T("%i"), i);
    
    		lvi.iItem = i;
    		lvi.iSubItem = 0;
    		lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
    		int index = m_cListCtrl.InsertItem(i, strItem);
    
    		strItem.Format(_T("%d"), 10*i);
    
    		m_cListCtrl.SetItem(index, 1, LVIF_TEXT, strItem, 0, 0, 0, 0);
    
    	}
    I spent more than 3 hours scratching my *** on this problem and all i needed to do was use 0th row to insert and not 1. changes set in bold.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Hmm.... a 5+ year old topic....

    Oh, well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 09-23-2008, 03:32 AM
  2. pointer comparison failing
    By Bleech in forum C Programming
    Replies: 4
    Last Post: 08-11-2007, 06:33 PM
  3. CreateDevice failing
    By MadCow257 in forum Game Programming
    Replies: 6
    Last Post: 03-14-2006, 09:03 PM
  4. initializes all components of failing to false
    By romeoz in forum C++ Programming
    Replies: 21
    Last Post: 08-01-2003, 09:30 PM
  5. failing hardware
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-10-2002, 10:14 AM