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. list control

list control

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
23 Posts 4 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.
  • D David Crow

    jokefake wrote:

    1. how do i delete an item from the list control

    By using the DeleteItem() method.

    jokefake wrote:

    2)how do i move an item up or down in the list contol using "up buttom" and "down buttom" i have created

    By deleting it and inserting it into the correct location.

    jokefake wrote:

    3)how do i make the "add buttom" disable when i have 8 items in the list control.

    By using the EnableWindow() method.


    "The largest fire starts but with the smallest spark." - David Crow

    "Judge not by the eye but by the heart." - Native American Proverb

    J Offline
    J Offline
    jokefake
    wrote on last edited by
    #5

    i did sth like this but it is not working void CSpeedDial::OnSpeedDelete() { POSITION pos = m_speedList.GetFirstSelectedItemPosition(); if(pos == NULL) return; int index = m_speedList.GetNextSelectedItem(pos); if(index<0 || index>m_speedList.GetItemCount()|| m_speedList.GetItemCount()!= m_speedArray.GetSize()) return; m_speedList.DeleteItem(index); m_speedArray.RemoveAt(index); // TODO: Add your control notification handler code here }

    D 1 Reply Last reply
    0
    • F FarPointer

      1)Try CListCtrl::DeleteItem 2)Try LVN_KEYDOWN Notification after adding notify_reflect 3)Try LVN_INSERTITEM Notification and keep a counter along with the LVN_DELETEITEM were you will decrement it and wen it becomes 8 disable it . Regards, FarPointer Blog:FARPOINTER

      J Offline
      J Offline
      jokefake
      wrote on last edited by
      #6

      i try sth like this but is not working void CSpeedDial::OnSpeedDelete() { POSITION pos = m_speedList.GetFirstSelectedItemPosition(); if(pos == NULL) return; int index = m_speedList.GetNextSelectedItem(pos); if(index<0 || index>m_speedList.GetItemCount()|| m_speedList.GetItemCount()!= m_speedArray.GetSize()) return; m_speedList.DeleteItem(index); m_speedArray.RemoveAt(index); // TODO: Add your control notification handler code here }

      1 Reply Last reply
      0
      • J jokefake

        i did sth like this but it is not working void CSpeedDial::OnSpeedDelete() { POSITION pos = m_speedList.GetFirstSelectedItemPosition(); if(pos == NULL) return; int index = m_speedList.GetNextSelectedItem(pos); if(index<0 || index>m_speedList.GetItemCount()|| m_speedList.GetItemCount()!= m_speedArray.GetSize()) return; m_speedList.DeleteItem(index); m_speedArray.RemoveAt(index); // TODO: Add your control notification handler code here }

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

        jokefake wrote:

        i did sth like this but it is not working

        So what's not working with it? You've got a lot of code there that needs to be narrowed down to just a single statement or two. Set breakpoints. Use the debugger. Step into the code to see what is going on. The first thing to try is this:

        void CSpeedDial::OnSpeedDelete()
        {
        m_speedList.DeleteItem(0);
        }

        If this works, then you know what the problem is not.


        "The largest fire starts but with the smallest spark." - David Crow

        "Judge not by the eye but by the heart." - Native American Proverb

        J 1 Reply Last reply
        0
        • D David Crow

          jokefake wrote:

          i did sth like this but it is not working

          So what's not working with it? You've got a lot of code there that needs to be narrowed down to just a single statement or two. Set breakpoints. Use the debugger. Step into the code to see what is going on. The first thing to try is this:

          void CSpeedDial::OnSpeedDelete()
          {
          m_speedList.DeleteItem(0);
          }

          If this works, then you know what the problem is not.


          "The largest fire starts but with the smallest spark." - David Crow

          "Judge not by the eye but by the heart." - Native American Proverb

          J Offline
          J Offline
          jokefake
          wrote on last edited by
          #8

          thanks it is now working

          D 1 Reply Last reply
          0
          • J jokefake

            thanks it is now working

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

            Why don't you share with others what the resolution was?


            "The largest fire starts but with the smallest spark." - David Crow

            "Judge not by the eye but by the heart." - Native American Proverb

            J 1 Reply Last reply
            0
            • D David Crow

              Why don't you share with others what the resolution was?


              "The largest fire starts but with the smallest spark." - David Crow

              "Judge not by the eye but by the heart." - Native American Proverb

              J Offline
              J Offline
              jokefake
              wrote on last edited by
              #10

              i just removed some of the lines from the code void CSpeedDial::OnSpeedDelete() { POSITION pos = m_speedList.GetFirstSelectedItemPosition(); int index = m_speedList.GetNextSelectedItem(pos); m_speedList.DeleteItem(index); // TODO: Add your control notification handler code here } but how can i move an item up and down in the list i am a beginner

              D 1 Reply Last reply
              0
              • J jokefake

                i just removed some of the lines from the code void CSpeedDial::OnSpeedDelete() { POSITION pos = m_speedList.GetFirstSelectedItemPosition(); int index = m_speedList.GetNextSelectedItem(pos); m_speedList.DeleteItem(index); // TODO: Add your control notification handler code here } but how can i move an item up and down in the list i am a beginner

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

                jokefake wrote:

                i just removed some of the lines from the code void CSpeedDial::OnSpeedDelete() { POSITION pos = m_speedList.GetFirstSelectedItemPosition(); int index = m_speedList.GetNextSelectedItem(pos); m_speedList.DeleteItem(index); // TODO: Add your control notification handler code here }

                Which can be shortened to:

                void CSpeedDial::OnSpeedDelete()
                {
                int index = m_speedList.GetNextItem(-1, LVIS_SELECTED);
                if (index != -1)
                m_speedList.DeleteItem(index);
                }

                jokefake wrote:

                but how can i move an item up and down in the list

                By deleting it and inserting it into the correct location.


                "The largest fire starts but with the smallest spark." - David Crow

                "Judge not by the eye but by the heart." - Native American Proverb

                J 1 Reply Last reply
                0
                • D David Crow

                  jokefake wrote:

                  i just removed some of the lines from the code void CSpeedDial::OnSpeedDelete() { POSITION pos = m_speedList.GetFirstSelectedItemPosition(); int index = m_speedList.GetNextSelectedItem(pos); m_speedList.DeleteItem(index); // TODO: Add your control notification handler code here }

                  Which can be shortened to:

                  void CSpeedDial::OnSpeedDelete()
                  {
                  int index = m_speedList.GetNextItem(-1, LVIS_SELECTED);
                  if (index != -1)
                  m_speedList.DeleteItem(index);
                  }

                  jokefake wrote:

                  but how can i move an item up and down in the list

                  By deleting it and inserting it into the correct location.


                  "The largest fire starts but with the smallest spark." - David Crow

                  "Judge not by the eye but by the heart." - Native American Proverb

                  J Offline
                  J Offline
                  jokefake
                  wrote on last edited by
                  #12

                  thanks, that code is really short and simple. I tried moving a selected item up in the list control, but it is not working. what may wrong with the code void CTestingDlg::OnUp() { int index = m_cListCtrl.GetNextItem(-1, LVIS_SELECTED); m_cListCtrl.InsertItem(index -1, m_cListCtrl.GetItemText(index, index)); m_cListCtrl.DeleteItem(index +1); // TODO: Add your control notification handler code here }

                  D 1 Reply Last reply
                  0
                  • J jokefake

                    thanks, that code is really short and simple. I tried moving a selected item up in the list control, but it is not working. what may wrong with the code void CTestingDlg::OnUp() { int index = m_cListCtrl.GetNextItem(-1, LVIS_SELECTED); m_cListCtrl.InsertItem(index -1, m_cListCtrl.GetItemText(index, index)); m_cListCtrl.DeleteItem(index +1); // TODO: Add your control notification handler code here }

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

                    Delete first, then insert.


                    "The largest fire starts but with the smallest spark." - David Crow

                    "Judge not by the eye but by the heart." - Native American Proverb

                    J 1 Reply Last reply
                    0
                    • D David Crow

                      Delete first, then insert.


                      "The largest fire starts but with the smallest spark." - David Crow

                      "Judge not by the eye but by the heart." - Native American Proverb

                      J Offline
                      J Offline
                      jokefake
                      wrote on last edited by
                      #14

                      deleting it first, deletes the row as i click on the buttom i extracted my code from here. BOOL CListCtrlEx::MoveRow(int from, int to) { //Can't move to the same place, or from or to a negative index if(from == to || from < 0 || to < 0) return FALSE; //First Copy the row to the new location if(CopyRow(from, to)) { //If we have just inserted a row before //this one in the list, we need to increment //our index. if(from > to) DeleteItem(from + 1); else DeleteItem(from); return TRUE; } else return FALSE; } BOOL CListCtrlEx::CopyRow(int from, int to) { //Can't move to the same place, or from or to a negative index if(from == to || from < 0 || to < 0) return FALSE; //Copy the row to the new index InsertItem(to, GetItemText(from, 0)); //If row has been inserted before original //increment the original if(from > to) from++; //Loop through subitems for(int i = 1; i < GetColumnCount(); i++) { SetItemText(to, i, GetItemText(from, i)); } return TRUE; }

                      D 1 Reply Last reply
                      0
                      • J jokefake

                        deleting it first, deletes the row as i click on the buttom i extracted my code from here. BOOL CListCtrlEx::MoveRow(int from, int to) { //Can't move to the same place, or from or to a negative index if(from == to || from < 0 || to < 0) return FALSE; //First Copy the row to the new location if(CopyRow(from, to)) { //If we have just inserted a row before //this one in the list, we need to increment //our index. if(from > to) DeleteItem(from + 1); else DeleteItem(from); return TRUE; } else return FALSE; } BOOL CListCtrlEx::CopyRow(int from, int to) { //Can't move to the same place, or from or to a negative index if(from == to || from < 0 || to < 0) return FALSE; //Copy the row to the new index InsertItem(to, GetItemText(from, 0)); //If row has been inserted before original //increment the original if(from > to) from++; //Loop through subitems for(int i = 1; i < GetColumnCount(); i++) { SetItemText(to, i, GetItemText(from, i)); } return TRUE; }

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

                        It's not a good idea to copy code from elsewhere unless you have a good understanding of what it is doing. If the code is doing a specific task and you copy it to a new location, its role will change.

                        void OnClickMoveUp( void )
                        {
                        int nIndex;
                        CString strCommand;

                        nIndex = m\_listctrl.GetNextItem(-1, LVNI\_SELECTED);
                        ASSERT(-1 != nIndex);
                        
                        strCommand  = m\_listctrl.GetItemText(nIndex, 0);
                        
                        m\_listctrl.DeleteItem(nIndex);
                        
                        nIndex = m\_listctrl.InsertItem(nIndex - 1, strCommand);
                        

                        }


                        "The largest fire starts but with the smallest spark." - David Crow

                        "Judge not by the eye but by the heart." - Native American Proverb

                        J 1 Reply Last reply
                        0
                        • D David Crow

                          It's not a good idea to copy code from elsewhere unless you have a good understanding of what it is doing. If the code is doing a specific task and you copy it to a new location, its role will change.

                          void OnClickMoveUp( void )
                          {
                          int nIndex;
                          CString strCommand;

                          nIndex = m\_listctrl.GetNextItem(-1, LVNI\_SELECTED);
                          ASSERT(-1 != nIndex);
                          
                          strCommand  = m\_listctrl.GetItemText(nIndex, 0);
                          
                          m\_listctrl.DeleteItem(nIndex);
                          
                          nIndex = m\_listctrl.InsertItem(nIndex - 1, strCommand);
                          

                          }


                          "The largest fire starts but with the smallest spark." - David Crow

                          "Judge not by the eye but by the heart." - Native American Proverb

                          J Offline
                          J Offline
                          jokefake
                          wrote on last edited by
                          #16

                          thanks, it works perfectly with a list with only one column, what about if you have 2 columns

                          D 1 Reply Last reply
                          0
                          • J jokefake

                            thanks, it works perfectly with a list with only one column, what about if you have 2 columns

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

                            jokefake wrote:

                            it works perfectly with a list with only one column, what about if you have 2 columns

                            Multiple calls to GetItemText(), for starters.


                            "The largest fire starts but with the smallest spark." - David Crow

                            "Judge not by the eye but by the heart." - Native American Proverb

                            J 1 Reply Last reply
                            0
                            • D David Crow

                              jokefake wrote:

                              it works perfectly with a list with only one column, what about if you have 2 columns

                              Multiple calls to GetItemText(), for starters.


                              "The largest fire starts but with the smallest spark." - David Crow

                              "Judge not by the eye but by the heart." - Native American Proverb

                              J Offline
                              J Offline
                              jokefake
                              wrote on last edited by
                              #18

                              i did sth like this and the result was no good at all void CTestingDlg::OnUp() { int nIndex; CString strCommand,strCommand2; nIndex = m_cListCtrl.GetNextItem(-1, LVNI_SELECTED); ASSERT(-1 != nIndex); strCommand = m_cListCtrl.GetItemText(nIndex, 0); strCommand2 = m_cListCtrl.GetItemText(0,nIndex); m_cListCtrl.DeleteItem(nIndex); nIndex = m_cListCtrl.InsertItem(nIndex - 1, strCommand); m_cListCtrl.SetItemText(nIndex-1,1, strCommand2); }

                              D 1 Reply Last reply
                              0
                              • J jokefake

                                i did sth like this and the result was no good at all void CTestingDlg::OnUp() { int nIndex; CString strCommand,strCommand2; nIndex = m_cListCtrl.GetNextItem(-1, LVNI_SELECTED); ASSERT(-1 != nIndex); strCommand = m_cListCtrl.GetItemText(nIndex, 0); strCommand2 = m_cListCtrl.GetItemText(0,nIndex); m_cListCtrl.DeleteItem(nIndex); nIndex = m_cListCtrl.InsertItem(nIndex - 1, strCommand); m_cListCtrl.SetItemText(nIndex-1,1, strCommand2); }

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

                                jokefake wrote:

                                strCommand2 = m_cListCtrl.GetItemText(0,nIndex);

                                Should be:

                                strCommand2 = m_cListCtrl.GetItemText(nIndex, 1);


                                "The largest fire starts but with the smallest spark." - David Crow

                                "Judge not by the eye but by the heart." - Native American Proverb

                                J 1 Reply Last reply
                                0
                                • D David Crow

                                  jokefake wrote:

                                  strCommand2 = m_cListCtrl.GetItemText(0,nIndex);

                                  Should be:

                                  strCommand2 = m_cListCtrl.GetItemText(nIndex, 1);


                                  "The largest fire starts but with the smallest spark." - David Crow

                                  "Judge not by the eye but by the heart." - Native American Proverb

                                  J Offline
                                  J Offline
                                  jokefake
                                  wrote on last edited by
                                  #20

                                  do i have to do changes to the last code. the text on the 2nd column are not showing m_cListCtrl.SetItemText(nIndex-1,1, strCommand2);

                                  D 1 Reply Last reply
                                  0
                                  • J jokefake

                                    do i have to do changes to the last code. the text on the 2nd column are not showing m_cListCtrl.SetItemText(nIndex-1,1, strCommand2);

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

                                    jokefake wrote:

                                    the text on the 2nd column are not showing

                                    What does SetItemText() return? Have you verified that strCommand2 contains anything?


                                    "The largest fire starts but with the smallest spark." - David Crow

                                    "Judge not by the eye but by the heart." - Native American Proverb

                                    J 1 Reply Last reply
                                    0
                                    • D David Crow

                                      jokefake wrote:

                                      the text on the 2nd column are not showing

                                      What does SetItemText() return? Have you verified that strCommand2 contains anything?


                                      "The largest fire starts but with the smallest spark." - David Crow

                                      "Judge not by the eye but by the heart." - Native American Proverb

                                      J Offline
                                      J Offline
                                      jokefake
                                      wrote on last edited by
                                      #22

                                      strCommand contains the correct string. i think the problem have to do with the SetWindowText. do you have any suggestion of what i can do. -- modified at 6:02 Wednesday 12th July, 2006

                                      D 1 Reply Last reply
                                      0
                                      • J jokefake

                                        strCommand contains the correct string. i think the problem have to do with the SetWindowText. do you have any suggestion of what i can do. -- modified at 6:02 Wednesday 12th July, 2006

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

                                        jokefake wrote:

                                        i think the problem have to do with the SetWindowText.

                                        SetWindowText() is not used with a list control.


                                        "The largest fire starts but with the smallest spark." - David Crow

                                        "Judge not by the eye but by the heart." - Native American Proverb

                                        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