Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can some one please tell me how to insert items into a friggin CListCtrl

Can some one please tell me how to insert items into a friggin CListCtrl

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10924543
    wrote on last edited by
    #1

    This method does not work:

    // Insert item for col 0
    lvi.mask = LVIF_TEXT;
    lvi.pszText = "one";
    m_ListCtrl.InsertItem(&lvi);
    // Insert subitem for col 1
    lvi.pszText = "two";
    lvi.iSubItem = 1;
    m_ListCtrl.InsertItem(&lvi);

    This method does not work:

    	for (int nI = 0; nI < m\_strarrayOtherImgFiles.GetCount(); nI++)
    	{
    		pOtherImgsList->SetItemText(nI, 0, m\_strarrayOtherImgFiles.GetAt(nI));
    	}
    

    Nothing seems to work.

    T L 2 Replies Last reply
    0
    • U User 10924543

      This method does not work:

      // Insert item for col 0
      lvi.mask = LVIF_TEXT;
      lvi.pszText = "one";
      m_ListCtrl.InsertItem(&lvi);
      // Insert subitem for col 1
      lvi.pszText = "two";
      lvi.iSubItem = 1;
      m_ListCtrl.InsertItem(&lvi);

      This method does not work:

      	for (int nI = 0; nI < m\_strarrayOtherImgFiles.GetCount(); nI++)
      	{
      		pOtherImgsList->SetItemText(nI, 0, m\_strarrayOtherImgFiles.GetAt(nI));
      	}
      

      Nothing seems to work.

      T Offline
      T Offline
      tagopi
      wrote on last edited by
      #2

      Hello, First, see the topic - 'How to ask Questions' in this forum. Second, you are missing lvi.iItem I think. Try adding this and see.

      // Insert item for col 0
      lvi.mask = LVIF_TEXT;
      lvi.iItem = 0;
      lvi.iSubItem = 0;
      lvi.pszText = "one";
      m_ListCtrl.InsertItem(&lvi);
      // Insert subitem for col 1
      lvi.pszText = "two";
      lvi.iSubItem = 1;
      m_ListCtrl.InsertItem(&lvi);

      Regards, A. Gopinath.

      1 Reply Last reply
      0
      • U User 10924543

        This method does not work:

        // Insert item for col 0
        lvi.mask = LVIF_TEXT;
        lvi.pszText = "one";
        m_ListCtrl.InsertItem(&lvi);
        // Insert subitem for col 1
        lvi.pszText = "two";
        lvi.iSubItem = 1;
        m_ListCtrl.InsertItem(&lvi);

        This method does not work:

        	for (int nI = 0; nI < m\_strarrayOtherImgFiles.GetCount(); nI++)
        	{
        		pOtherImgsList->SetItemText(nI, 0, m\_strarrayOtherImgFiles.GetAt(nI));
        	}
        

        Nothing seems to work.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You could always try reading the documentation[^], or finding one of the many articles, such as: Using the List Control[^].

        U 1 Reply Last reply
        0
        • L Lost User

          You could always try reading the documentation[^], or finding one of the many articles, such as: Using the List Control[^].

          U Offline
          U Offline
          User 10924543
          wrote on last edited by
          #4

          I have been reading the documentation - where else do you think I obtained those methods that I have been trying. This is what I have tried precisely. And function

          int InsertItem(
          int nItem,
          LPCTSTR lpszItem

          ,as described here (http://msdn.microsoft.com/en-us/library/8b9s12fc.aspx[^]), DOES NOT WORK The function does nothing. WHY? Can you only add items to a CListCtrl via

          int InsertItem(
          const LVITEM* pItem
          );

          and the function that I have been using is in fact defunct?

          **void CImageDialog::DDX_List(CDataExchange* pDX, UINT uListCtrlID, CStringArray& rarrayOtherImgFiles)
          {
          CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(uListCtrlID);
          int nI = 0;
          CString strItem;

          ASSERT\_VALID(pListCtrl);
          
          if (pDX->m\_bSaveAndValidate)
          {
          	m\_arrayOtherImageFiles.RemoveAll();
          	for (nI = 0; nI < pListCtrl->GetItemCount(); nI++)
          	{
          		strItem = pListCtrl->GetItemText(nI, 0);
          		m\_arrayOtherImageFiles.Add(strItem);
          	}
          }
          else
          {
          	pListCtrl->DeleteAllItems();
          	for (nI = m\_arrayOtherImageFiles.GetCount() - 1; nI >= 0; nI--)
          	{
          		pListCtrl->InsertItem(1, m\_arrayOtherImageFiles.GetAt(nI));
          	}
          }
          

          }**

          void CImageDialog::DoDataExchange(CDataExchange* pDX)
          {
          CMyDialog::DoDataExchange(pDX);
          DDX_Text(pDX, IDC_EDIT_HEADER_IMG, m_strHeaderImageFile);
          DDX_Text(pDX, IDC_EDIT_NAV_IMG1, m_strNavImg1File);
          DDX_Text(pDX, IDC_EDIT_NAV_IMG2, m_strNavImg2File);
          DDX_Text(pDX, IDC_EDIT_OTHER_IMG_FILES, m_strOtherImgFiles);
          DDX_List(pDX, IDC_LIST_OTHER_IMG_FILES, m_arrayOtherImageFiles);
          }

          L 1 Reply Last reply
          0
          • U User 10924543

            I have been reading the documentation - where else do you think I obtained those methods that I have been trying. This is what I have tried precisely. And function

            int InsertItem(
            int nItem,
            LPCTSTR lpszItem

            ,as described here (http://msdn.microsoft.com/en-us/library/8b9s12fc.aspx[^]), DOES NOT WORK The function does nothing. WHY? Can you only add items to a CListCtrl via

            int InsertItem(
            const LVITEM* pItem
            );

            and the function that I have been using is in fact defunct?

            **void CImageDialog::DDX_List(CDataExchange* pDX, UINT uListCtrlID, CStringArray& rarrayOtherImgFiles)
            {
            CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(uListCtrlID);
            int nI = 0;
            CString strItem;

            ASSERT\_VALID(pListCtrl);
            
            if (pDX->m\_bSaveAndValidate)
            {
            	m\_arrayOtherImageFiles.RemoveAll();
            	for (nI = 0; nI < pListCtrl->GetItemCount(); nI++)
            	{
            		strItem = pListCtrl->GetItemText(nI, 0);
            		m\_arrayOtherImageFiles.Add(strItem);
            	}
            }
            else
            {
            	pListCtrl->DeleteAllItems();
            	for (nI = m\_arrayOtherImageFiles.GetCount() - 1; nI >= 0; nI--)
            	{
            		pListCtrl->InsertItem(1, m\_arrayOtherImageFiles.GetAt(nI));
            	}
            }
            

            }**

            void CImageDialog::DoDataExchange(CDataExchange* pDX)
            {
            CMyDialog::DoDataExchange(pDX);
            DDX_Text(pDX, IDC_EDIT_HEADER_IMG, m_strHeaderImageFile);
            DDX_Text(pDX, IDC_EDIT_NAV_IMG1, m_strNavImg1File);
            DDX_Text(pDX, IDC_EDIT_NAV_IMG2, m_strNavImg2File);
            DDX_Text(pDX, IDC_EDIT_OTHER_IMG_FILES, m_strOtherImgFiles);
            DDX_List(pDX, IDC_LIST_OTHER_IMG_FILES, m_arrayOtherImageFiles);
            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Of course the function works, it is used in List controls all over the world. It is more likely that your code is doing something wrong. For a start you are not checking the return code to see whether it succeeded or not, and if not, why. What is the current value of the index nI, and what is being returned by the call to m_arrayOtherImageFiles.GetAt(nI)? Use your debugger to trace through your code to diagnose the problem.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups