Solved

Walking thru a CListCtrl

Posted on 1997-12-23
5
1,022 Views
Last Modified: 2013-11-19
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..



0
Comment
Question by:jcorbin
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 100 total points
ID: 1313003
LV_ITEM item has iItem which defines row number and iSubItem wich is zero based index of column. To retreive information of first row and second column for instance declare on stack instance LV_ITEM, set iItem = 0 and iSubItem = 1. Then call CListCtrl::GetItem  
0
 

Author Comment

by:jcorbin
ID: 1313004
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
0
 
LVL 1

Expert Comment

by:WBerthin
ID: 1313005
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.

0
 

Author Comment

by:jcorbin
ID: 1313006
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

0
 
LVL 1

Expert Comment

by:WBerthin
ID: 1313007
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
substring method in java 1 79
while loop over for loop 7 81
noX challenge 17 77
Detect file exist or not 3 83
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now