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. How to add Item to ListCtrl

How to add Item to ListCtrl

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
27 Posts 5 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.
  • CPalliniC CPallini

    A simple line like the following

    myListCtrl.InsertItem(0,_T("foo"));

    Should do the magic. See also this documentation page for a complete example http://msdn2.microsoft.com/en-us/library/8b9s12fc(VS.80).aspx[^]. What was your code and what was the exception occurred? :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    [my articles]

    T Offline
    T Offline
    tina newcoder
    wrote on last edited by
    #3

    I tried ur statement, but don't work. the list i created have 4 columns, i created them as follows, m_users.InsertColumn(1,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(2,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(3,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(4,"d",LVCFMT_CENTER,150,0); the columns are created very well, but then i m trying to add data as m_users.InsertItem(0,_T("foo")); but there is nothing to be seen in the list ! plz koi help karona !!!

    modified on Saturday, January 12, 2008 9:21:40 AM

    D H CPalliniC J 4 Replies Last reply
    0
    • T tina newcoder

      I tried ur statement, but don't work. the list i created have 4 columns, i created them as follows, m_users.InsertColumn(1,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(2,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(3,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(4,"d",LVCFMT_CENTER,150,0); the columns are created very well, but then i m trying to add data as m_users.InsertItem(0,_T("foo")); but there is nothing to be seen in the list ! plz koi help karona !!!

      modified on Saturday, January 12, 2008 9:21:40 AM

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #4

      tina- wrote:

      m_users.InsertColumn(1,"a",LVCFMT_CENTER,150,0);

      Why are you not starting with 0?

      tina- wrote:

      m_users.InsertItem(0,_T("foo"));

      What does this return?

      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      T 1 Reply Last reply
      0
      • T tina newcoder

        I tried ur statement, but don't work. the list i created have 4 columns, i created them as follows, m_users.InsertColumn(1,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(2,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(3,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(4,"d",LVCFMT_CENTER,150,0); the columns are created very well, but then i m trying to add data as m_users.InsertItem(0,_T("foo")); but there is nothing to be seen in the list ! plz koi help karona !!!

        modified on Saturday, January 12, 2008 9:21:40 AM

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #5

        Hey I found two good articles for you here[^] and here[^]. ;)

        1 Reply Last reply
        0
        • T tina newcoder

          I tried ur statement, but don't work. the list i created have 4 columns, i created them as follows, m_users.InsertColumn(1,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(2,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(3,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(4,"d",LVCFMT_CENTER,150,0); the columns are created very well, but then i m trying to add data as m_users.InsertItem(0,_T("foo")); but there is nothing to be seen in the list ! plz koi help karona !!!

          modified on Saturday, January 12, 2008 9:21:40 AM

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #6

          Maybe you need to post more code. The code you posted so far actually works on my system. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          [my articles]

          In testa che avete, signor di Ceprano?

          H 1 Reply Last reply
          0
          • D David Crow

            tina- wrote:

            m_users.InsertColumn(1,"a",LVCFMT_CENTER,150,0);

            Why are you not starting with 0?

            tina- wrote:

            m_users.InsertItem(0,_T("foo"));

            What does this return?

            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            T Offline
            T Offline
            tina newcoder
            wrote on last edited by
            #7

            @ DavidCrow m_users.InsertItem(0,_T("foo")); is returning 0

            D 1 Reply Last reply
            0
            • CPalliniC CPallini

              Maybe you need to post more code. The code you posted so far actually works on my system. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              [my articles]

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #8

              Maybe you have the special compiler that it can understands! ;P

              CPalliniC 1 Reply Last reply
              0
              • H Hamid Taebi

                Maybe you have the special compiler that it can understands! ;P

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #9

                Hamid. wrote:

                Maybe you have the special compiler that it can understands!

                Shhhhhhhhhhhh!... Don't make they know. :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                [my articles]

                In testa che avete, signor di Ceprano?

                T 1 Reply Last reply
                0
                • T tina newcoder

                  I tried ur statement, but don't work. the list i created have 4 columns, i created them as follows, m_users.InsertColumn(1,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(2,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(3,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(4,"d",LVCFMT_CENTER,150,0); the columns are created very well, but then i m trying to add data as m_users.InsertItem(0,_T("foo")); but there is nothing to be seen in the list ! plz koi help karona !!!

                  modified on Saturday, January 12, 2008 9:21:40 AM

                  J Offline
                  J Offline
                  Jorgen Sigvardsson
                  wrote on last edited by
                  #10

                  My bet is that because you inserted a new column at 1, an empty zero width column was created at column 0. The inserted item is there, but it's in the 0th column, which you can't see. Try inserting columns from index 0 instead! :)

                  -- Kein Mitleid Für Die Mehrheit

                  CPalliniC 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Hamid. wrote:

                    Maybe you have the special compiler that it can understands!

                    Shhhhhhhhhhhh!... Don't make they know. :-D

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    [my articles]

                    T Offline
                    T Offline
                    tina newcoder
                    wrote on last edited by
                    #11

                    wat was that ???? :confused:

                    CPalliniC 1 Reply Last reply
                    0
                    • J Jorgen Sigvardsson

                      My bet is that because you inserted a new column at 1, an empty zero width column was created at column 0. The inserted item is there, but it's in the 0th column, which you can't see. Try inserting columns from index 0 instead! :)

                      -- Kein Mitleid Für Die Mehrheit

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #12

                      Jörgen Sigvardsson wrote:

                      Try inserting columns from index 0 instead

                      Or add SubItems. Good bet indeed. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      [my articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      • T tina newcoder

                        wat was that ???? :confused:

                        CPalliniC Offline
                        CPalliniC Offline
                        CPallini
                        wrote on last edited by
                        #13

                        Don't worry about. Just kidding with my old friend Hamid. BTW try to start insertion from column 0, as suggested by Jörgen Sigvardsson. :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        [my articles]

                        In testa che avete, signor di Ceprano?

                        1 Reply Last reply
                        0
                        • T tina newcoder

                          I have created a ClistCtrl variable and added 4 columns to that list. Now I want to add Item to that list, I tried using InsertItem() member function, but it either doesn't work or gives an excemption. :confused: Please help me, i have alos tried using MSDN, but its all gone in vain ! :((

                          T Offline
                          T Offline
                          tina newcoder
                          wrote on last edited by
                          #14

                          m_users.InsertColumn(0,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(1,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(2,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(3,"d",LVCFMT_CENTER,150,0); LVITEM lvi; lvi.mask=LVIF_TEXT | LVIF_NORECOMPUTE ; lvi.iItem = 1; lvi.iSubItem = 0; lvi.pszText = _T("Hi"); m_users.InsertItem(&lvi); on compilation & execution, still nothing on List ! Let me give you some more details, I might have done something wrong there... I have used VC++ Wizard to create my project (i m new to vc++) Then I added List control to a Dialog box Then changed its properties to ->Report Then created a m_users variable Then I have inserted 4 columns to it Everything works fine till here... Now I try to add some data to it, & nothing happens on the list ! plz friends, guide me in this....

                          CPalliniC 1 Reply Last reply
                          0
                          • T tina newcoder

                            m_users.InsertColumn(0,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(1,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(2,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(3,"d",LVCFMT_CENTER,150,0); LVITEM lvi; lvi.mask=LVIF_TEXT | LVIF_NORECOMPUTE ; lvi.iItem = 1; lvi.iSubItem = 0; lvi.pszText = _T("Hi"); m_users.InsertItem(&lvi); on compilation & execution, still nothing on List ! Let me give you some more details, I might have done something wrong there... I have used VC++ Wizard to create my project (i m new to vc++) Then I added List control to a Dialog box Then changed its properties to ->Report Then created a m_users variable Then I have inserted 4 columns to it Everything works fine till here... Now I try to add some data to it, & nothing happens on the list ! plz friends, guide me in this....

                            CPalliniC Offline
                            CPalliniC Offline
                            CPallini
                            wrote on last edited by
                            #15

                            Were you cheating about the effective overload of the InsertItem method called? :suss: Anyway: Some suggestions: (1) Change

                            tina- wrote:

                            m_users.InsertColumn(0,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(1,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(2,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(3,"d",LVCFMT_CENTER,150,0);

                            To

                            m_users.InsertColumn(0,"a",LVCFMT_CENTER,150,0);
                            m_users.InsertColumn(1,"b",LVCFMT_CENTER,50,1);
                            m_users.InsertColumn(2,"c",LVCFMT_CENTER,150,2);
                            m_users.InsertColumn(3,"d",LVCFMT_CENTER,150,3);

                            (2) Change

                            tina- wrote:

                            lvi.iItem = 1;

                            To

                            lvi.iItem = 0;

                            (3) Add, before calling InsertItem method

                            lvi.cchTextMax = _tcslen(_T("Hi"));

                            :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            [my articles]

                            In testa che avete, signor di Ceprano?

                            T 2 Replies Last reply
                            0
                            • CPalliniC CPallini

                              Were you cheating about the effective overload of the InsertItem method called? :suss: Anyway: Some suggestions: (1) Change

                              tina- wrote:

                              m_users.InsertColumn(0,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(1,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(2,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(3,"d",LVCFMT_CENTER,150,0);

                              To

                              m_users.InsertColumn(0,"a",LVCFMT_CENTER,150,0);
                              m_users.InsertColumn(1,"b",LVCFMT_CENTER,50,1);
                              m_users.InsertColumn(2,"c",LVCFMT_CENTER,150,2);
                              m_users.InsertColumn(3,"d",LVCFMT_CENTER,150,3);

                              (2) Change

                              tina- wrote:

                              lvi.iItem = 1;

                              To

                              lvi.iItem = 0;

                              (3) Add, before calling InsertItem method

                              lvi.cchTextMax = _tcslen(_T("Hi"));

                              :)

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              [my articles]

                              T Offline
                              T Offline
                              tina newcoder
                              wrote on last edited by
                              #16

                              Thanx for your reply, but I made the changes & its still not working !

                              CPalliniC H 2 Replies Last reply
                              0
                              • T tina newcoder

                                @ DavidCrow m_users.InsertItem(0,_T("foo")); is returning 0

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #17

                                tina- wrote:

                                m_users.InsertItem(0,_T("foo")); is returning 0

                                Which indicates that the item is being added.

                                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                1 Reply Last reply
                                0
                                • T tina newcoder

                                  Thanx for your reply, but I made the changes & its still not working !

                                  CPalliniC Offline
                                  CPalliniC Offline
                                  CPallini
                                  wrote on last edited by
                                  #18

                                  Try to further populate the List, for instance, add

                                  BOOL fSuccess;
                                  INT nItem;
                                  fSuccess = m_users.SetItemText(0,1,_T("foo1");
                                  fSuccess = m_users.SetItemText(0,2,_T("foo2");
                                  fSuccess = m_users.SetItemText(0,3,_T("foo3");
                                  nItem = m_users.InsertItem( 1, _T("boo"));
                                  fSuccess = m_users.SetItemText(1,1,_T("boo1");
                                  fSuccess = m_users.SetItemText(1,2,_T("boo2");
                                  fSuccess = m_users.SetItemText(1,3,_T("boo3");

                                  Checking always, with the debugger, the return values. My resources are near to end... :)

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  [my articles]

                                  In testa che avete, signor di Ceprano?

                                  T 1 Reply Last reply
                                  0
                                  • T tina newcoder

                                    Thanx for your reply, but I made the changes & its still not working !

                                    H Offline
                                    H Offline
                                    Hamid Taebi
                                    wrote on last edited by
                                    #19

                                    Did you see my links?

                                    T 1 Reply Last reply
                                    0
                                    • CPalliniC CPallini

                                      Try to further populate the List, for instance, add

                                      BOOL fSuccess;
                                      INT nItem;
                                      fSuccess = m_users.SetItemText(0,1,_T("foo1");
                                      fSuccess = m_users.SetItemText(0,2,_T("foo2");
                                      fSuccess = m_users.SetItemText(0,3,_T("foo3");
                                      nItem = m_users.InsertItem( 1, _T("boo"));
                                      fSuccess = m_users.SetItemText(1,1,_T("boo1");
                                      fSuccess = m_users.SetItemText(1,2,_T("boo2");
                                      fSuccess = m_users.SetItemText(1,3,_T("boo3");

                                      Checking always, with the debugger, the return values. My resources are near to end... :)

                                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                      [my articles]

                                      T Offline
                                      T Offline
                                      tina newcoder
                                      wrote on last edited by
                                      #20

                                      i tried to insert ur code, > CPallini wrote: > > BOOL fSuccess; INT nItem; fSuccess = m_users.SetItemText(0,1,_T("foo1")); fSuccess = m_users.SetItemText(0,2,_T("foo2")); fSuccess = m_users.SetItemText(0,3,_T("foo3")); nItem = m_users.InsertItem( 1, _T("boo")); fSuccess = m_users.SetItemText(1,1,_T("boo1")); fSuccess = m_users.SetItemText(1,2,_T("boo2")); fSuccess = m_users.SetItemText(1,3,_T("boo3")); but its giving exception !!!

                                      CPalliniC 1 Reply Last reply
                                      0
                                      • H Hamid Taebi

                                        Did you see my links?

                                        T Offline
                                        T Offline
                                        tina newcoder
                                        wrote on last edited by
                                        #21

                                        Hamid. wrote:

                                        Did you see my links?

                                        yeah, i checked ur links. the code is working very well there, but not in my project! wat shall i do ??? :confused:

                                        H 1 Reply Last reply
                                        0
                                        • CPalliniC CPallini

                                          Were you cheating about the effective overload of the InsertItem method called? :suss: Anyway: Some suggestions: (1) Change

                                          tina- wrote:

                                          m_users.InsertColumn(0,"a",LVCFMT_CENTER,150,0); m_users.InsertColumn(1,"b",LVCFMT_CENTER,50,0); m_users.InsertColumn(2,"c",LVCFMT_CENTER,150,0); m_users.InsertColumn(3,"d",LVCFMT_CENTER,150,0);

                                          To

                                          m_users.InsertColumn(0,"a",LVCFMT_CENTER,150,0);
                                          m_users.InsertColumn(1,"b",LVCFMT_CENTER,50,1);
                                          m_users.InsertColumn(2,"c",LVCFMT_CENTER,150,2);
                                          m_users.InsertColumn(3,"d",LVCFMT_CENTER,150,3);

                                          (2) Change

                                          tina- wrote:

                                          lvi.iItem = 1;

                                          To

                                          lvi.iItem = 0;

                                          (3) Add, before calling InsertItem method

                                          lvi.cchTextMax = _tcslen(_T("Hi"));

                                          :)

                                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                          [my articles]

                                          T Offline
                                          T Offline
                                          tina newcoder
                                          wrote on last edited by
                                          #22

                                          CPallini wrote:

                                          Were you cheating about the effective overload of the InsertItem method called?

                                          i didn't get u ... :confused:

                                          CPalliniC 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