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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. why does CListCtrl.InsertItem return -1?

why does CListCtrl.InsertItem return -1?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
3 Posts 2 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.
  • T Offline
    T Offline
    Trucker
    wrote on last edited by
    #1

    Hello, I am having a weird problem with a list view control (CListCtrl) in MFC. I can create it but for some reason I can't add an item to it. When I call the controls InsertItem function, it returns -1, which according to the MFC documentation means that adding the item to the list view was unsuccessful. I know this from running this code: //creating listview folderContents.Create(WS_VISIBLE|WS_CHILD|WS_BORDER| LVS_LIST|LVS_SINGLESEL, CRect(10,10,300,180), this, IDC_FOLDERCONTENTS); LV_ITEM lvItem; lvItem.mask = LVIF_TEXT|LVIF_STATE; lvItem.state = 0; lvItem.stateMask = 0; lvItem.pszText = "an item"; lvItem.iItem = 0; //add the item to the listview. If unsuccessful //then change background color of the list view if(folderContents.InsertItem(&lvItem) == -1) { COLORREF c = PALETTERGB(150, 90, 130); folderContents.SetBkColor(c); } When I run the application, backgound color of the list view (named folderContents in above code) is changed. So why cant InsertItem add an item to the list view? There is no explanation in the documentation. Anybody know?:confused: TraileR ParK LifE 4Ever

    C 1 Reply Last reply
    0
    • T Trucker

      Hello, I am having a weird problem with a list view control (CListCtrl) in MFC. I can create it but for some reason I can't add an item to it. When I call the controls InsertItem function, it returns -1, which according to the MFC documentation means that adding the item to the list view was unsuccessful. I know this from running this code: //creating listview folderContents.Create(WS_VISIBLE|WS_CHILD|WS_BORDER| LVS_LIST|LVS_SINGLESEL, CRect(10,10,300,180), this, IDC_FOLDERCONTENTS); LV_ITEM lvItem; lvItem.mask = LVIF_TEXT|LVIF_STATE; lvItem.state = 0; lvItem.stateMask = 0; lvItem.pszText = "an item"; lvItem.iItem = 0; //add the item to the listview. If unsuccessful //then change background color of the list view if(folderContents.InsertItem(&lvItem) == -1) { COLORREF c = PALETTERGB(150, 90, 130); folderContents.SetBkColor(c); } When I run the application, backgound color of the list view (named folderContents in above code) is changed. So why cant InsertItem add an item to the list view? There is no explanation in the documentation. Anybody know?:confused: TraileR ParK LifE 4Ever

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      The LV_ITEM structure contains a lot of info apart from what you're specifying, and it's all full of random data when you create one. If you add this line: ::memset(&lvItem, 0, sizeof(LV_ITEM)); before setting any values, it works ( I just tried it :-) ) Christian Graus - Microsoft MVP - C++

      T 1 Reply Last reply
      0
      • C Christian Graus

        The LV_ITEM structure contains a lot of info apart from what you're specifying, and it's all full of random data when you create one. If you add this line: ::memset(&lvItem, 0, sizeof(LV_ITEM)); before setting any values, it works ( I just tried it :-) ) Christian Graus - Microsoft MVP - C++

        T Offline
        T Offline
        Trucker
        wrote on last edited by
        #3

        Wow! Thanks a lot! I did not know about this function.:rolleyes: TraileR ParK LifE 4Ever

        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