Question : SetItem in CListCtrl failing to set text

I'm using the CViewCtrl derived class from Code Guru as
a CListCtrl. I've used this twice before, and in trying
to use it a third time I keep coming across a problem
where using the SetItem function does not work correctly.

The call is returning 1, which indicates success, but if I
immediately do a GetItemText on the item/subitem I set, the
string that is returned is empty.

I cannot work out why identical code would work in one
place and not in another.

The InsertItem call to insert the row DOES work, so the
label is set, but any other columns are not being set,
although the function says they are.

Here is an example of the code to setup the columns (all
three columns are displayed):



      UpdateData(TRUE);

      // Setup the list control - uses class derived from list control
      LV_COLUMN      column;
      LPSTR            colheaderstr;
      int                  retval;

      // Column 0
      NLSLoadString(IDS_AUTOITPL_PVGEN_COL1,colheaderstr,NULL);
      column.mask            = LVCF_TEXT | LVCF_WIDTH;
      column.cx            = PRDVGEN_CPP_DATE_COL_WIDTH;
      column.pszText      = (LPSTR) colheaderstr;
      retval = m_HistoryPeriods.InsertColumn(0,&column);

      // Column 1
      NLSLoadString(IDS_AUTOITPL_PVGEN_COL2,colheaderstr,NULL);
      column.mask            = LVCF_TEXT | LVCF_WIDTH;
      column.cx            = PRDVGEN_CPP_VAL_COL_WIDTH;
      column.pszText      = (LPSTR) colheaderstr;
      retval = m_HistoryPeriods.InsertColumn(1,&column);

      // Column 2
      NLSLoadString(IDS_AUTOITPL_PVGEN_COL3,colheaderstr,NULL);
      column.mask            = LVCF_TEXT | LVCF_WIDTH;
      column.cx            = PRDVGEN_CPP_PERIODS_COL_WIDTH;
      column.pszText      = (LPSTR) colheaderstr;
      retval = m_HistoryPeriods.InsertColumn(2,&column);

      UpdateData(FALSE);

And here is an example of how I am inserting a row. I've checked that pszText is being set correctly.

      // Inserts a row into the list control
      LV_ITEM            item;
      int                  retval = 0;

      // Insert date
      item.mask            = LVIF_TEXT;
      item.iItem            = index;
      item.iSubItem      = 0;
      item.pszText      = (LPSTR) date;
      retval = m_HistoryPeriods.InsertItem(&item);            // Use InsertItem here

      // Insert value
      item.mask            = LVIF_TEXT;
      item.iItem            = index;
      item.iSubItem      = 1;
      item.pszText      = (LPSTR) value;
      retval = m_HistoryPeriods.SetItem(&item);                  // For extra columns, use SetItem

      // Insert number of periods
      item.mask            = LVIF_TEXT;
      item.iItem            = index;
      item.iSubItem      = 2;
      item.pszText      = (LPSTR) pds;
      retval = m_HistoryPeriods.SetItem(&item);                  // For extra columns, use SetItem


I've also tried using SetItemText, but this produces the
same results.

Any ideas why this would fail are welcome.

Thanks.

Answer : SetItem in CListCtrl failing to set text

Hi Jamessiddle,
I always use InsertItem for the first column and for the other colums I use SetItemText. Check out this little sample:

CRect rect;
int   iColWidth;

// set column width (-1 pixel otherwise a nasty scrollbar
// appears...
//
m_list.GetWindowRect( &rect );
iColWidth = ((rect.right - rect.left ) /3) -1;
   
// create the column headers
//
m_list.InsertColumn( 0, "Name", LVCFMT_LEFT, iColWidth, 1);
m_list.InsertColumn( 1, "Address", LVCFMT_LEFT, iColWidth, 1);
m_list.InsertColumn( 2, "Place", LVCFMT_LEFT, iColWidth, 1);



LVITEM lvitem;

lvitem.mask = LVIF_TEXT;
lvitem.iItem = 0;
lvitem.iSubItem = 0;
CString strTest;
int i;


for( i = 0; i< 10 ; i++ )
{

   lvitem.iItem = i;

   // insert an item in the first column
   //
   strTest.Format( "name %d", i);
   lvitem.pszText = (LPSTR)(LPCSTR)strTest;
   lvitem.cchTextMax = sizeof( lvitem.pszText);      
   m_list.InsertItem( &lvitem );
     
   // second column
   //
   strTest.Format( "address %d", i);
   m_list.SetItemText( i, 1, (LPSTR)(LPCTSTR)strTest );

   // third column
   strTest.Format( "place %d", i);
   m_list.SetItemText( i, 2, (LPSTR)(LPCTSTR)strTest );

}

Random Solutions  
 
programming4us programming4us