Link to home
Start Free TrialLog in
Avatar of jcorbin
jcorbin

asked on

Walking thru a CListCtrl

How can I get the values of each row's columns and add them to an array so I can refer to them in my app when needed?? I have a CListCtrl that will have 5 columns per row and ??? rows. I know I can get the number of rows from CLitsCtrl.GetItemCount(), but the GetItem macro has me a bit baffled, I would think that it would take an Index as a parameter but it takes a LV_ITEM structure. Need explained..



ASKER CERTIFIED SOLUTION
Avatar of galkin
galkin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jcorbin
jcorbin

ASKER

I tried this to get the contents of the ListCtrl and write it to a file

        int j;
      char myCol;
      LV_ITEM * myRow;
      CFileException e;
      CString FilePath = "crontab.txt";
      CStdioFile f;

      if(!f.Open(FilePath, CFile::modeCreate|CFile::modeWrite|CFile::typeText|CFile::modeNoTruncate, &e))
      {
            afxDump << "File could not be opened " << e.m_cause << "\n";
      }
      
      for (j=0;j<m_CronList.GetItemCount();j++)
      {
      f.SeekToEnd();
      myRow->iItem = j;
      myRow->iSubItem = 0;
      myCol = m_CronList.GetItem(myRow);

But myCol is returning junk, is my declaration of the LV_ITEM correct?? Sorry I am a VB & Java programmer, just learning to use MFC..

John
I have successfully used the CListCtrl function
CString GetItemText( int nItem, int nSubItem ) const;
to do this, type of thing.

(read nItem as row and nSubItem as Column)

Let me know if you need to see more code.

Avatar of jcorbin

ASKER

Yes I'd like to see the code, I tried this

CString myRow;
myRow = m_MyListView.GetItemText(1,1);

and it returned nothing even tho there is text in the 1st and 2nd columns....

John

I cannot immediately see the problem with trying
CString myRow = m_MyListView.GetItemText(1,1);

BUT I'll see if I can help here, John.
Is the m_MyListView in your code a object of CListCtrl type?

My code below copies the full contents of a list control item
from a source item to a destination item including all
columns (in report style). The code looks like this
and works just fine for me:

//THE SOURCE ITEM'S INDEX IS nSource
//THE DESTINATION ITEM'S INDEX IS nDestination
//m_ListControl IS A CListCtrl OBJECT
//IN A CLASS DERIVED FROM CView

//GET THE INFORMATION ON THE ITEM TO BE COPIED
char szLabel[256];
LV_ITEM lvi;
ZeroMemory(&lvi, sizeof(LV_ITEM));
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM;
lvi.stateMask = LVIS_DROPHILITED | LVIS_FOCUSED | LVIS_SELECTED;
lvi.pszText = szLabel;
lvi.iItem = nSource;
lvi.cchTextMax = 255;
m_ListControl.GetItem(&lvi);

//AT THIS POINT lvi CONTAINS THE INFORMATION ON THE ITEM
//TO BE COPIED

//CREATE THE NEW ITEM
lvi.iItem = nDestination;
m_ListControl.InsertItem(&lvi);

//NOW THE NEW ITEM CONTAINS A COPY OF THE INFORMATION
//EXCEPT FOR THE COLUMN INFORMATION IN REPORT VIEW

//THEN I EXTRACT THE TEXT INFORMATION COLUMN BY
//COLUMN FROM ONE ITEM AND COPY IT TO THE OTHER
CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
lvi.mask = LVIF_TEXT;
lvi.iItem = nDestination;
for(int col=1; col<nColumnCount; col++)
{
    strcpy(lvi.pszText,                                                (LPCTSTR)(m_ListControl.GetItemText(nSource, col)));
    lvi.iSubItem = col;
    m_ListControl.SetItem(&lvi);
)

//NOW nDestination IS A COMPLETE COPY OF nSource
//INCLUDING ALL COLUMNS

I assume
    strcpy(lvi.pszText,                                                (LPCTSTR)(m_ListControl.GetItemText(nSource, col)));
could copy those columns into any char* or CString array
with minor modification.

Please let me know how you make out with that.