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. Error deleting first column in CListCtrl

Error deleting first column in CListCtrl

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelpquestion
8 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.
  • M Offline
    M Offline
    Member 868926
    wrote on last edited by
    #1

    Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:

    Header1 Header2 Header3

    Data1 Data2 Data3

    After trying to delete first column:

    Header2 Header3

    Data1 Data2

    Having created the columns to begin with, I'm setting up the rest of the table like this:

    void MyClass::InsertItem(int x, int y, CString &str)
    {

    LVITEM lvi;
    lvi.mask = LVIF\_TEXT;
    lvi.iItem = y;
    lvi.iSubItem = x;
    lvi.pszText = (LPTSTR)(LPCTSTR)(str);
    int result;
    if(x == 0)    {
        result = mCSVListCtrl.InsertItem(&lvi); 
    }
    else {
        result = mCSVListCtrl.SetItem(&lvi); 
    }
    

    }

    which seems to create the table OK. Then when a header is clicked:

    void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
    {
    LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);

    mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
    
    \*pResult = 0;
    

    }

    Any ideas?

    R D J V 5 Replies Last reply
    0
    • M Member 868926

      Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:

      Header1 Header2 Header3

      Data1 Data2 Data3

      After trying to delete first column:

      Header2 Header3

      Data1 Data2

      Having created the columns to begin with, I'm setting up the rest of the table like this:

      void MyClass::InsertItem(int x, int y, CString &str)
      {

      LVITEM lvi;
      lvi.mask = LVIF\_TEXT;
      lvi.iItem = y;
      lvi.iSubItem = x;
      lvi.pszText = (LPTSTR)(LPCTSTR)(str);
      int result;
      if(x == 0)    {
          result = mCSVListCtrl.InsertItem(&lvi); 
      }
      else {
          result = mCSVListCtrl.SetItem(&lvi); 
      }
      

      }

      which seems to create the table OK. Then when a header is clicked:

      void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
      {
      LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);

      mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
      
      \*pResult = 0;
      

      }

      Any ideas?

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      Either look using the debugger or put in a trace statement but make sure the sub item value is what you expect it to be. That's the only idea I have because I know that calling DeleteColumn(0) works because I do it often. One of the weird things with the list control is you can't set the first column to be right-justified so the work-around is to add one more column than needed, set column 1 to be right-justified, and then delete column 0. BTW - this was true with older versions and I don't know if it is still necessary. I do it anyway because it still works.

      M 1 Reply Last reply
      0
      • M Member 868926

        Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:

        Header1 Header2 Header3

        Data1 Data2 Data3

        After trying to delete first column:

        Header2 Header3

        Data1 Data2

        Having created the columns to begin with, I'm setting up the rest of the table like this:

        void MyClass::InsertItem(int x, int y, CString &str)
        {

        LVITEM lvi;
        lvi.mask = LVIF\_TEXT;
        lvi.iItem = y;
        lvi.iSubItem = x;
        lvi.pszText = (LPTSTR)(LPCTSTR)(str);
        int result;
        if(x == 0)    {
            result = mCSVListCtrl.InsertItem(&lvi); 
        }
        else {
            result = mCSVListCtrl.SetItem(&lvi); 
        }
        

        }

        which seems to create the table OK. Then when a header is clicked:

        void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
        {
        LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);

        mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
        
        \*pResult = 0;
        

        }

        Any ideas?

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

        What happens if you create a "dummy" column 0 with a width of 0 (or maybe 1)?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • M Member 868926

          Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:

          Header1 Header2 Header3

          Data1 Data2 Data3

          After trying to delete first column:

          Header2 Header3

          Data1 Data2

          Having created the columns to begin with, I'm setting up the rest of the table like this:

          void MyClass::InsertItem(int x, int y, CString &str)
          {

          LVITEM lvi;
          lvi.mask = LVIF\_TEXT;
          lvi.iItem = y;
          lvi.iSubItem = x;
          lvi.pszText = (LPTSTR)(LPCTSTR)(str);
          int result;
          if(x == 0)    {
              result = mCSVListCtrl.InsertItem(&lvi); 
          }
          else {
              result = mCSVListCtrl.SetItem(&lvi); 
          }
          

          }

          which seems to create the table OK. Then when a header is clicked:

          void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
          {
          LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);

          mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
          
          \*pResult = 0;
          

          }

          Any ideas?

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          Not a solution to the problem but a tip. Instead of deleting columns you might set the width to zero. Then you can implement a hide/show column handler (which requires storing the actual or a default width for each column when hiding to be restored when showing it again). Note also that it might be necessary to get the effective column index when the columns has been reordered (GetColumnOrderArray).

          1 Reply Last reply
          0
          • R Rick York

            Either look using the debugger or put in a trace statement but make sure the sub item value is what you expect it to be. That's the only idea I have because I know that calling DeleteColumn(0) works because I do it often. One of the weird things with the list control is you can't set the first column to be right-justified so the work-around is to add one more column than needed, set column 1 to be right-justified, and then delete column 0. BTW - this was true with older versions and I don't know if it is still necessary. I do it anyway because it still works.

            M Offline
            M Offline
            Member 868926
            wrote on last edited by
            #5

            Thanks for the various replies. The debugger doesn't go beyond DeleteColumn() but I can see it's passing zero. Can you see anything odd about the way I'm creating my sub items that could be causing the problem? I've created the columns first then for each item (row) I'm adding subitems left to right. Otherwise I'll have to go for the dummy first column option.

            1 Reply Last reply
            0
            • M Member 868926

              Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:

              Header1 Header2 Header3

              Data1 Data2 Data3

              After trying to delete first column:

              Header2 Header3

              Data1 Data2

              Having created the columns to begin with, I'm setting up the rest of the table like this:

              void MyClass::InsertItem(int x, int y, CString &str)
              {

              LVITEM lvi;
              lvi.mask = LVIF\_TEXT;
              lvi.iItem = y;
              lvi.iSubItem = x;
              lvi.pszText = (LPTSTR)(LPCTSTR)(str);
              int result;
              if(x == 0)    {
                  result = mCSVListCtrl.InsertItem(&lvi); 
              }
              else {
                  result = mCSVListCtrl.SetItem(&lvi); 
              }
              

              }

              which seems to create the table OK. Then when a header is clicked:

              void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
              {
              LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);

              mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
              
              \*pResult = 0;
              

              }

              Any ideas?

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              Member 868926 wrote:

              when a header is clicked:

              void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
              {
              LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
               
              mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
               
              *pResult = 0;
              }

              Any ideas?

              Just for a test: what will happen if you call

              mCSVListCtrl.DeleteColumn(0);

              from anywhere but from this MyClass::OnLvnColumnclickList(...) message handler?

              M 1 Reply Last reply
              0
              • M Member 868926

                Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:

                Header1 Header2 Header3

                Data1 Data2 Data3

                After trying to delete first column:

                Header2 Header3

                Data1 Data2

                Having created the columns to begin with, I'm setting up the rest of the table like this:

                void MyClass::InsertItem(int x, int y, CString &str)
                {

                LVITEM lvi;
                lvi.mask = LVIF\_TEXT;
                lvi.iItem = y;
                lvi.iSubItem = x;
                lvi.pszText = (LPTSTR)(LPCTSTR)(str);
                int result;
                if(x == 0)    {
                    result = mCSVListCtrl.InsertItem(&lvi); 
                }
                else {
                    result = mCSVListCtrl.SetItem(&lvi); 
                }
                

                }

                which seems to create the table OK. Then when a header is clicked:

                void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
                {
                LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);

                mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
                
                \*pResult = 0;
                

                }

                Any ideas?

                V Offline
                V Offline
                Victor Nijegorodov
                wrote on last edited by
                #7

                Member 868926 wrote:

                Any ideas?

                Additional info: [LVM_DELETECOLUMN message](https://msdn.microsoft.com/en-us/library/windows/desktop/bb774894(v=vs.85).aspx)

                1 Reply Last reply
                0
                • V Victor Nijegorodov

                  Member 868926 wrote:

                  when a header is clicked:

                  void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
                  {
                  LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
                   
                  mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
                   
                  *pResult = 0;
                  }

                  Any ideas?

                  Just for a test: what will happen if you call

                  mCSVListCtrl.DeleteColumn(0);

                  from anywhere but from this MyClass::OnLvnColumnclickList(...) message handler?

                  M Offline
                  M Offline
                  Member 868926
                  wrote on last edited by
                  #8

                  Victor Nijegorodov wrote:

                  Just for a test: what will happen if you call

                  mCSVListCtrl.DeleteColumn(0);

                  from anywhere but from this MyClass::OnLvnColumnclickList(...) message handler?

                  It's the same. I think I'll have to go for the dummy left column idea some of you have suggested - maybe I'll use it to number the rows which might be quite nice anyway. Thanks again for all your replies.

                  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